ArrayList Reverse Iteration Example

ArrayList Iteration:

[java highlight=”16,18,19″]
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());
}

}
}
[/java]

Output:
[plain gutter=”false”]
ArrayList Iteration:

Sachin
Gambhir
Dhone
Sehwag
Raina
Yuvraj
[/plain]

ArrayList Reverse Iteration:
[java highlight=”16,18,19″]
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());
}

}
}
[/java]

Output:
[plain gutter=”false”]
ArrayList Reverse Iteration:

Yuvraj
Raina
Sehwag
Dhone
Gambhir
Sachin
[/plain]

Recommended Books:

Leave a Reply