ArrayList listIterator(int index) method in java code example
Example 1: ArrayList listIterator(int index) method in java
example on listIterator(int index) method IndexOutOfBoundsException
import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorIndexMethodExample
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add("apple");
al.add("pineapple");
al.add("orange");
al.add("watermelon");
al.add("banana");
System.out.println("ArrayList of fruits: " + al);
ListIterator lif = al.listIterator(1);
ListIterator lib = al.listIterator(6);
// using hasNext() and next() methods to iterate in forward direction
System.out.println("Iterate in forward direction from second position: ");
while(lif.hasNext())
{
System.out.println(lif.next());
}
// using hasPrevious() and previous() methods to iterate in backward direction
System.out.println("Iterate in backward direction from second position: ");
while(lib.hasPrevious())
{
System.out.println(lib.previous());
}
}
}
Example 2: ArrayList listIterator(int index) method in java
import java.util.ArrayList;
import java.util.ListIterator;
public class ArrayListListIteratorIndexMethodExample
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add("apple");
al.add("pineapple");
al.add("orange");
al.add("watermelon");
al.add("banana");
System.out.println("ArrayList of fruits: " + al);
ListIterator lif = al.listIterator(1);
ListIterator lib = al.listIterator(2);
// using hasNext() and next() methods to iterate in forward direction
System.out.println("Iterate in forward direction from second position: ");
while(lif.hasNext())
{
System.out.println(lif.next());
}
// using hasPrevious() and previous() methods to iterate in backward direction
System.out.println("Iterate in backward direction from second position: ");
while(lib.hasPrevious())
{
System.out.println(lib.previous());
}
}
}