ArrayList class is one of many collection classes that
the standard Java library supplies




Collection<String> coll = new ArrayList<>();
|
The ArrayList class implements the
Collectioninterface. |
coll = new TreeSet<String>(); |
The TreeSet class also implements the Collection
interface. |
int n = coll.size(); |
Gets the size of the collection. n is now 0. |
coll.add("Harry"); |
Adds elements to the collection. |
String s = coll.toString(); |
Returns a string with all elements in the collection. s
is now "[Harry, Sally]" |
System.out.println(coll); |
Invokes the toString method and prints [Harry,
Sally]. |
coll.remove("Harry"); |
Checks whether this collection contains a given element.
b is now true. |
for (String s : coll) |
You can use the “for each” loop with any collection. This loop
prints the elements on separate lines. |
Iterator<> iter = coll.iterator(); |
You use an iterator for visiting the elements in the collection |
You are taking a social science class and you are assigned lots of books and articles as required reading. Which data structure is best for organizing them?



LinkedList<String> list = new
LinkedList<>(); |
An empty list. |
list.addLast("Harry"); |
Adds an element to the end of the list. Same as add. |
list.addFirst("Sally"); |
Adds an element to the beginning of the list. list is now
[Sally, Harry]. |
list.getFirst(); |
Gets the element stored at the beginning of the list; here
"Sally". |
list.getLast(); |
Gets the element stored at the end of the list; here
"Harry". |
String removed = list.removeFirst(); |
Removes the first element of the list and returns it. removed is
"Sally" and list is [Harry]. Use
removeLast to remove the last element. |
ListIterator<> iter=list.listIterator() |
Provides an iterator for visiting all list elements |

String s = iter.next(); |
Assume that iter points to the beginning of the list
[Sally] before calling next. After the call, s is
"Sally" and the iterator points to the end. |
iter.previous(); iter.set("Juliet"); |
The set method updates the last element returned by next or previous.
The list is now [Juliet]. |
iter.hasNext() |
Returns false because the iterator is at the end of the
collection. |
if (iter.hasPrevious()) { s = iter.previous(); } |
hasPrevious returns true because the iterator is not at
the beginning of the list. previous and
hasPrevious are ListIterator methods. |
iter.add("Diana"); |
Adds an element before the iterator position (ListIterator only). The
list is now [Diana, Juliet]. |
iter.next(); iter.remove(); |
remove removes the last element returned by next or previous. The
list is now [Diana]. |
while (iterator.hasNext())
{
String name = iterator.next();
if (condition is fulfilled for name)
{
iterator.remove();
}
}
|
this loop removes all names that fulfill a certain condition: |
What is the contents of letters after this sequence of
instructions?
List<String> letters = new LinkedList<>();
letters.add("F");
letters.add("R");
letters.add("E");
letters.add("D");
ListIterator<String> iter = letters.iterator();
iter.next();
iter.next();
iter.remove();
iter.next();
iter.add("X");