ArrayList Facts and Methods to Know
3 ways to initialize an arraylist:
· Create the ArrayList and add items to it later using the add method:
o ArrayList<Integer> atest = new ArrayList<Integer>();
o atest.add(1);
· Create the ArrayList with a collection of a list created using Arrays.aslist.
o yList<Integer> dtest = new ArrayList<Integer>(Arrays.asList(1,5,1,5));
· Create the ArrayList with an anonymous Arraylist subclass:
o ArrayList<Integer> dtestans = new ArrayList<Integer>( ){{ add(1); add(6); add(1);add(6);}}
Using an ArrayList
Gotcha: Remember there is an array under the ArrayList,
and its indexes are maintained whenever movements are made inside the array. ArrayList will resize to the number of elements after every
move.
This is a subset of methods described at: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
boolean |
Appends the specified element to the end of
this list. |
void |
Inserts the specified element at the
specified position in this list. |
boolean |
addAll(Collection<? extends E> c) Appends all of the elements in the specified
collection to the end of this list, in the order that they are returned by
the specified collection's Iterator. |
boolean |
addAll(int index,
Collection<? extends E> c) Inserts all of the elements in the specified
collection into this list, starting at the specified position. |
void |
clear() Removes all of the elements from this list. |
boolean |
Returns true if this list contains the specified
element. |
get(int index) Returns the element at the specified
position in this list. |
|
int |
Returns the index of the first occurrence of
the specified element in this list, or -1 if this list does not contain the
element. |
boolean |
isEmpty() Returns true if this list contains no elements. |
int |
Returns the index of the last occurrence of
the specified element in this list, or -1 if this list does not contain the
element. |
remove(int index) Removes the element at the specified
position in this list. |
|
boolean |
Removes the first occurrence of the
specified element from this list, if it is present. |
boolean |
removeAll(Collection<?> c) Removes from this list all of its elements
that are contained in the specified collection. |
boolean |
retainAll(Collection<?> c) Retains only the elements in this list that
are contained in the specified collection. |
Replaces the element at the specified
position in this list with the specified element. |
|
int |
size() Returns the number of elements in this list. |
Object[] |
toArray() Returns an array containing all of the
elements in this list in proper sequence (from first to last element). |
Read through with a For Each Loop: (Works with Primitive Arrays also) https://blog.udemy.com/for-each-loop-java/
for ( ElementType variable : ArrayListVariable) {
// do your work with variable – but you cannot change it
}
Example follows:
ArrayList <String> arr = new ArrayList<String>(Arrays.asList("a","b","c"));
for (String s : arr){
System.out.print(s);
}
Note on capacity: While capacity methods will allocate space for changes you intend to make to the array, you wont see the capacity changes reflected in the size.