ArrayList: iterator() : ArrayList « java.util « Java by API






ArrayList: iterator()

  
/**
 *Output: 
Original contents of al: C A E B D F 
Modified list backwards: F+ D+ B+ E+ A+ C+ 

 */

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

public class MainClass {
  public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    System.out.print("Original contents of al: ");
    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
      String element = itr.next();
      System.out.print(element + " ");
    }
    System.out.println();

    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
      String element = litr.next();
      litr.set(element + "+");
    }

    // Now, display the list backwards.
    System.out.print("Modified list backwards: ");
    while (litr.hasPrevious()) {
      String element = litr.previous();
      System.out.print(element + " ");
    }
  }
}

           
         
    
  








Related examples in the same category

1.new ArrayList < E > ()
2.new ArrayList(Collection c)
3.ArrayList: add(E o)
4.ArrayList: add(int index, E element)
5.ArrayList: addAll(Collection c)
6.ArrayList: addAll(int index, Collection c)
7.ArrayList: clear()
8.ArrayList: contains(Object elem)
9.ArrayList: equals(Object o)
10.ArrayList: ensureCapacity(int minCapacity)
11.ArrayList: get(int index)
12.ArrayList: indexOf(Object elem)
13.ArrayList: isEmpty()
14.ArrayList: lastIndexOf(Object o)
15.ArrayList: listIterator()
16.ArrayList: remove(int index)
17.ArrayList: remove(Object o)
18.ArrayList: removeAll(Collection c)
19.ArrayList: retainAll(Collection c)
20.ArrayList: set(int index, T element)
21.ArrayList: size()
22.AbstractList: subList(int fromIndex, int toIndex)
23.ArrayList: toArray()
24.ArrayList: toArray(T[] a)
25.ArrayList: trimToSize()
26.for each loop for ArrayList