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:
[plain gutter=”false”]
ArrayList Iteration:

Sachin
Gambhir
Dhone
Sehwag
Raina
Yuvraj
[/plain]

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:
[plain gutter=”false”]
ArrayList Reverse Iteration:

Yuvraj
Raina
Sehwag
Dhone
Gambhir
Sachin
[/plain]

Recommended Books:

795 total views, 3 views today

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.