Wednesday, September 22, 2010

ArrayList<E> - Some basic methods and constructors

java.util.ArrayList allows for expandable arrays, and is basically the same as the older the Collections Vector class.

ArrayList extends AbstractList and implements List, Cloneable, SerializableArrayList capacity .grows automatically. The ArrayList is not synchronized. It permits all elements including null.
An ArrayList has these characteristics:
  • An ArrayList automatically expands as data is added.
  • Access to any element of an ArrayList is O(1). Insertions and deletions are O(N).
  • An ArrayList has methods for inserting, deleting, and searching.
  • An ArrayList can be traversed using a foreach loop, iterators, or indexes.
Implementation. ArrayLists are implemented with an underlying array, and when that array is full and an additional element is added, a new, larger, array is allocated and the elements are copied from the old to the new. Because it takes time to create a bigger array and copy the elements from the old array to the new array, it is a slightly faster to create an ArrayList with a size that it will commonly be when full. Of course, if you knew the final size, you could simply use an array. However, for non-critical sections of code programmers typically don't specify an initial size.

Common ArrayList methods and constructors

Here are some of the most useful ArrayList methods. Assume these declarations. Note that E is the notation for the type of an element in a collection. Sun recommends using single upper case letters for generic types.
int i;
ArrayList<E> a;
E e;
Iterator<E> iter;
E[] earray;
Object[] oarray;
Result Method Description
a = new ArrayList() Creates ArrayList with initial default capacity 10.
a = new ArrayList(cap) Creates ArrayList with initial int capacity cap.
a = new ArrayList(coll) Creates ArrayList from the Collection coll.
Adding elements
a.add(e) adds e to end of ArrayList a
a.add(i, e) Inserts e at index i, shifting elements up as necessary.
Replacing an element
a.set(i,e) Sets the element at index i to e.
Getting the elements
e = a.get(i) Returns the object at index i.
oarray = a.toArray() Returns values in array of objects.
earray = a.toArray(E[]) The array parameter should be of the E class. Returns values in that array (or a larger array is allocated if necessary).
iter = a.iterator() Returns an Iterator for forward traversal.
liter = a.listIterator(i) Returns a ListIterator for forward / backward / modifying traversal, starting at index i. Start from end with a.listIterator(a.size())
liter = a.listIterator() Returns a ListIterator for forward / backward / modifying traversal.
b = a.contains(e) Returns true if ArrayList a contains e
i = a.indexOf(e) Returns index of first occurrence of e, or -1 if not there.
i = a.lastIndexOf(e) Returns index of last occurrence of e, or -1 if not there.
Removing elements
a.clear() removes all elements from ArrayList a
a.remove(i) Removes the element at position i.
a.removeRange(i, j) Removes the elements from positions i thru j.
i = a.size() Returns the number of elements in ArrayList a.


Adding elements to the end of an ArrayList, getting them by index

ArrayList<E> a = new ArrayList<E>();  // Default size.
E s;          // Declare s to be an object type E.
. . .
a.add(s);     // Adds s to the end of the ArrayList a
. . .
s = a.get(i); // Assigns ith element from a to s.

No comments:

Post a Comment