arraylist listiterator 28int index 29 method in java

Solutions on MaxInterview for arraylist listiterator 28int index 29 method in java by the best coders in the world

showing results for - "arraylist listiterator 28int index 29 method in java"
Karl
12 Jan 2021
1import java.util.ArrayList;
2import java.util.ListIterator;
3public class ArrayListListIteratorIndexMethodExample
4{
5   public static void main(String[] args)
6   {
7      ArrayList<String> al = new ArrayList<String>();
8      al.add("apple");
9      al.add("pineapple");
10      al.add("orange");
11      al.add("watermelon");
12      al.add("banana");
13      System.out.println("ArrayList of fruits: " + al);
14      ListIterator<String> lif = al.listIterator(1);
15      ListIterator<String> lib = al.listIterator(2);
16      // using hasNext() and next() methods to iterate in forward direction
17      System.out.println("Iterate in forward direction from second position: ");
18      while(lif.hasNext())
19      {
20         System.out.println(lif.next());
21      }
22      // using hasPrevious() and previous() methods to iterate in backward direction
23      System.out.println("Iterate in backward direction from second position: ");
24      while(lib.hasPrevious())
25      {
26         System.out.println(lib.previous());
27      }
28   }
29}
Jessica
29 Jul 2019
1example on listIterator(int index) method IndexOutOfBoundsException
2import java.util.ArrayList;
3import java.util.ListIterator;
4public class ArrayListListIteratorIndexMethodExample
5{
6   public static void main(String[] args)
7   {
8      ArrayList<String> al = new ArrayList<String>();
9      al.add("apple");
10      al.add("pineapple");
11      al.add("orange");
12      al.add("watermelon");
13      al.add("banana");
14      System.out.println("ArrayList of fruits: " + al);
15      ListIterator<String> lif = al.listIterator(1);
16      ListIterator<String> lib = al.listIterator(6);
17      // using hasNext() and next() methods to iterate in forward direction
18      System.out.println("Iterate in forward direction from second position: ");
19      while(lif.hasNext())
20      {
21         System.out.println(lif.next());
22      }
23      // using hasPrevious() and previous() methods to iterate in backward direction
24      System.out.println("Iterate in backward direction from second position: ");
25      while(lib.hasPrevious())
26      {
27         System.out.println(lib.previous());
28      }
29   }
30}