- In addition to the standard collection methods, the List interface allows for random access of elements through its get and set methods.
- List indices are zero based, so list.get(list.size())) is not a valid element.
List stuff = new ArrayList();
stuff.add("Apple");
stuff.add("Orange");
// what's the last item? Indexes are zero based!
System.out.println(stuff.get(stuff.size() - 1));
// change the first item to a pear.
stuff.set(0, "Pear");
// now iterate
Iterator it = stuff.iterator();
while(it.hasNext())
{
String item = (String)it.next();
System.out.println(item);
}
- ListIterator allows element addition, removal, and replacement during iteration.
- The java.util package provides two new List implementations: ArrayList and LinkedList.
- Vector is a legacy class retrofitted to the new Collection framework, and is thread safe.
Next: Maps
Up: Collections framework
Previous: Collections framework
  Contents
|