The list interface adds random access methods:
interface List<T> extends Collection<T> {
int indexOf(T x); // = -1 if x not an
element
T get(int i); // get element at
position i
void add(int i, T o); // adds x at
position i
void remove(int i); // remove element
at position i
ListIterator<T> listIterator();
// get a 2-way iterator
// etc.
}
Array lists and linked lists are the major implementations of the interface:
List<String> list1 = new ArrayList<String>();
List<String> list2 = new LinkedList<String>();
List<String> list3 = new Vector<String>();
List<String> list4 = new LinkedList<String>();
The following list of planets contain duplicates:
String[] vals2 =
{"Mercury",
"Venus", "Earth", "Mars", "Jupiter",
"Venus"};
The order of a list is always the insertion order:
for(int i = 0; i < vals2.length; i++) {
list1.add(vals2[i]);
list2.add(vals2[i]);
list3.add(vals2[i]);
}
We can verify this by printing the lists:
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
which produces the result:
[Mercury, Venus, Earth, Mars, Jupiter, Venus]
[Mercury, Venus, Earth, Mars, Jupiter, Venus]
[Mercury, Venus, Earth, Mars, Jupiter, Venus]