1. Exam Points
ArrayList methods: get(), size(), add(), remove(), set()- For an ArrayList,
indexes are 0 to size -1.- For the
add(int index, E obj)method, 0 <= index <= size.- Create an ArrayList:
ArrayList<E> list=new ArrayList<E>();- When you try to
remove elements in a loop, the size and i will change.- When you try to remove elements in an ArrayList,
remove reversely.- Remember to
compare String valuesusing theequals()method.
2. Knowledge Points
(1) ArrayList vs. Arrays
- An
arrayis a collection with afixed length(number of elements), an array can storeboth primitive values or object references. - Example:
int[] nums = new int[10]; Dog[] dogs = new Dog[20]; - An
ArrayListismutable in sizeandcontains only object references. - Example:
ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); ArrayList list3 = new ArrayList(); ArrayList list4 = new ArrayList();
(2) Create ArrayList Objects
-
The ArrayList constructor ArrayList() constructs an empty list.
-
Java allows the generic type
ArrayList<E>, where the type parameter E specifies the type of the elements. -
Example:
// For an ArrayList of type Integer, E is Integer ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(10); Integer x = list1.get(0); // For an ArrayList of type Dog, E is Dog ArrayList<Dog> list2 = new ArrayList<Dog>(); list2.add(new Dog("Lucky")); Dog dog = list2.get(0); -
ArrayList<E>is preferred over ArrayList, E represents a data type. -
Example:
// Without specifying E, an ArrayList can contain objects of any type. ArrayList list1 = new ArrayList(); list1.add(10); list1.add(true); list1.add("Annabelle");// With specifying E, an ArrayList can contain only objects of type E. ArrayList<Integer> list2 = new ArrayList<Integer>(); list2.add(10); list2.add(20);
(3) ArrayList Methods
- The
indexesfor an ArrayList are0 to size - 1. ArrayList Methods:- int size(): returns the number of elements in the list.
- boolean add(E obj): appends obj to end of list.
- void add(int index, E obj): inserts obj at position index (0 <= index <= size).
- E get(int index): get the element at position index in the list.
- E set(int index, E obj): replaces the element at position index with obj; returns the former element at position index.
- E remove(int index): removes element from position index; returns the removed element.
- Example:
ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(10); list1.add(20); ArrayList<String> list2 = new ArrayList<String>(); list2.add("Amy"); list2.add("Anna"); list2.add("David"); // Note: The indexes for an ArrayList start at 0 and end at size - 1. // 1. int size() int size = list1.size(); // 2. boolean add(E obj): add one element to the end of the list list1.add(30); list2.add("Judy"); // 3. void add(int index, E obj) list1.add(0, 666); // 4. E get(int index) int x = list1.get(0); String y = list2.get(1); // 1 means the second element // 5. E set(int index, E obj): replace the element at a specified position list2.set(1, "Annabelle"); // 6. E remove(int index) list2.remove(0);
(4) Traverse ArrayLists
- You can traverse an ArrayList using
for, for-each, whileloops. - Example: Traverse ArrayLists using
for loops
ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(10); list1.add(20); list1.add(30); list1.add(40); ArrayList<String> list2 = new ArrayList<String>(); list2.add("Amy"); list2.add("Anna"); list2.add("David"); ArrayList<Dog> dogList = new ArrayList<Dog>(); dogList.add(new Dog("haha")); dogList.add(new Dog("lele")); dogList.add(new Dog("doudou")); // for loop: use indexes for (int i = 0; i < list1.size(); i++) { Integer x = list1.get(i); System.out.print(x + " "); } System.out.println(); for (int i = 0; i < list2.size(); i++) { String x = list2.get(i); System.out.print(x + " "); } System.out.println(); for (int i = 0; i < dogList.size(); i++) { Dog d = dogList.get(i); System.out.println(d.getName()); } - Example: Traverse ArrayLists using
for-each loops
// for-each loop: no need to use indexes for (Integer x : list1) { // x means the current element System.out.print(x + " "); } System.out.println(); for (String name : list2) { // x means the current element System.out.print(name + " "); } for(Dog x:dogList) { System.out.println(x.getName()); }
(5) ArrayLists as Parameters
- An
parametercan be ofArrayListtype. - A returned value from a method can be of ArrayList type.
- If you pass an
ArrayListobjectas an argumentto a parameter of a method,changes will reflect on the ArrayList. - Example:
static void replace(ArrayList<Integer> list, int index, int value) { list.set(index, value); } public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); list1.add(1); list1.add(2); // if you pass an ArrayList object as an argument to a parameter // of a method, changes will reflect on the ArrayList. System.out.println(list1); replace(list1, 0, 666); System.out.println(list1); }
(6) Remove Elements in Loops
- If you try to
remove elementsin a loop, remove the elementsreversely. - Example:
ArrayList<Integer> list = new ArrayList<Integer>(); list.add(-10); list.add(20); list.add(30); list.add(-40); // Example: remove negative elements in an ArrayList for(int i=list.size()-1;i>=0;i--) { if (list.get(i) < 0) { list.remove(i); } }