Basics
- Java uses ArrayList<T> to represent a dynamic list whose elements values of type T.
- For example, class ArrayList<String> can represent list whose elements are string values.
- The ArrayList<T> default constructor ArrayList<T>() configures a new ArrayList<T> to be an empty list.
- For example, the following definition makes list represent an empty list of type ArrayList<T>.
ArrayList<T> list = new ArrayList<T>();
Important methods
- Suppose list is an ArrayList<T>. Also, suppose v is a value of type T and i is an int.
- list.add( v ) adds an element with value v to the end of list.
- list.clear() removes all elements from list.
- list.contains( v ) returns whether value v equals one of the element values in list.
- list.get( i ) returns the value of the element with index i in list.
- list.indexOf( v ) returns the index of the first element in list with value v. If there is no such element, returns -1.
- list.isEmpty() returns whether list has any elements.
- list.lastIndexOf( v ) returns the index of the last element in list with value v. If there is no such element, returns -1.
- list.remove( i ) removes the element with index i from list.
- list.remove( v ) removes the first element equal to v from list.
- list.set( i, v ) sets the element in list with index i to have value v.
- list.size() returns the number of elements in list.
- list.toString() returns a String representation of the elements in list.
- list.toArray() returns an Object[] array containing all the elements in list.