List Interface
The
Listis implemented by both theArrayListand theLinkedList.
This page assumes that you already understand the material on the Java Collection Framework page.
List Interface
- The
List<E>interface describes a contract for classes that provide an implementation for storing lists of information. - The E between the angle brackets indicates the type of references contained in the list. For example
List<String>indicates that the list contains references toStrings. - Some relevant methods include:
add(E)— adds an object to the end of the list.clear()— removes all elements from the list.contains(E)— returns true if the specified element is found in the list.get(int)— returns the element at the specified location in the list.isEmpty()— returns true if no elements are in the list.set(int, E)— replaces the element at the specified location with the specified element.size()— returns the number of elements in the list.
- The following method does some silly stuff with a list of strings:
public static void silly(List<String> words) { if(!words.isEmpty()) { String word = words.get(0); words.add(word.trim()); words.set(words.size()-1, "I'm last"); } for(String word : words) { System.out.println(word); } } - I've introduced a new kind of for loop in the above code. If you read it like this: "For each
String, word, in theListofStrings, words, do the following: { blah blah blah }" you pretty much know what it does. - The loop will run
words.size()times. - The first time in the loop, word is a reference to the first element in the list.
- The second time in the loop, word is a reference to the second element in the list.
- ...
- The last time in the loop, word is a reference to the last element in the list.
- Of course, all of this is pretty useless if we don't know about any classes that implement the
List<E>interface, since we can't do:List<String> words = new List<>();
List Implementations
- The JCF provides a few classes that implement the
List<E>interface. - The
ArrayList<E>class.- This is basically a souped up array.
- You can think of this class as containing one field that is a reference to an array and a number of methods that implement the
List<E>interface. - There is really a bit more than this going on here, and you'll learn more about that.
- A simplified version of the
ArrayList<E>class might look something like this:public class ArrayList<E> implements List<E> { private E[] array; public boolean isEmpty() { return (null == array); } public void clear() { array = null; } public E get(int index) { return array[index]; } public void set(int index, E obj) { array[index] = obj; } public int size() { return array.length; } public boolean contains(E target) { boolean found = false; for(int i=0; !found && i < array.length; ++i) { found = target.equals(array[i]); } return found; } public boolean add(E obj) { E[] newArray = (E[])new Object[array.length+1]; for(int i=0; i < array.length; ++i) { newArray[i] = array[i]; } newArray[newArray.length-1] = obj; array = newArray; return true; } } // end of ArrayList class
- Again, this is a very simplified version of what really goes on.
- The
LinkedList<E>class. - This class also implements the
Listinterface. - Although you can interact with the class in the same way you would interact with an
ArrayList<E>, the way that each class implements the functionality described by theListinterface is quite different. - The
LinkedList<E>doesn't use an array to store the data internally. - We will study the internal structure of the
LinkedList<E>in CSC-1120. - For now, it is sufficient to know that the
LinkedList<E>class implements theListinterface. - Depending on your application, an
ArrayList<E>may be appropriate. Other times it may be better to use aLinkedList<E>. - Since both class implement the
Listinterface, you can easily modify which implementation you use by just changing the new operation. - The following code shows how you can use the silly method implemented above with either of these classes:
List<String> words = new ArrayList<>(); // do a bunch of .add calls to populate the list silly(words); words = new LinkedList<String>(); // do a bunch of .add calls to populate the list silly(words);