ArrayList Reverse Iteration Example

ArrayList Reverse Iteration Example

ArrayList Iteration:

package in.javadomain;

import java.util.ArrayList;
import java.util.ListIterator;

public class ReverseIteration {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("Sachin");
		list.add("Gambhir");
		list.add("Dhone");
		list.add("Sehwag");
		list.add("Raina");
		list.add("Yuvraj");
		ListIterator<String> li = list.listIterator();
		System.out.println("ArrayList Iteration: \n");
		while (li.hasNext()) {
			System.out.println(li.next());
		}

	}
}

Output:

ArrayList Iteration: 

Sachin
Gambhir
Dhone
Sehwag
Raina
Yuvraj

ArrayList Reverse Iteration:

package in.javadomain;

import java.util.ArrayList;
import java.util.ListIterator;

public class ReverseIteration {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("Sachin");
		list.add("Gambhir");
		list.add("Dhone");
		list.add("Sehwag");
		list.add("Raina");
		list.add("Yuvraj");
		ListIterator<String> li = list.listIterator(list.size());
		System.out.println("ArrayList Reverse Iteration: \n");
		while (li.hasPrevious()) {
			System.out.println(li.previous());
		}

	}
}

Output:

ArrayList Reverse Iteration: 

Yuvraj
Raina
Sehwag
Dhone
Gambhir
Sachin

Recommended Books:

464 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *