Chapter 4 |
ArrayLists
In chapter 2, we used Java standard arrays to store sequences of values. The greatest disadvantage of arrays is that they are fixed-size structures (we have to declare in advance how many elements we want to store in an array). After arrays are created, they cannot grow or shrink, which means that we must know in advance how many elements an array will hold. But sometimes we may not know how large arrays we need. To handle this situation, use an ArrayList.
An ArrayList is a dynamic structure with a variable length, so it can grow and shrink as the program is being executed. This means that:
- we can add an item at any point in the ArrayList container and the list size expands automatically to accommodate the new item;
- we can remove an item at any point from the ArrayList container and the array size contracts automatically.
Hence, Arraylist is an ordered collection (by index).
To use the ArrayList class, you must use the following import statement:
import java.util.ArrayList;
Then, declare an ArrayList, using the default constructor. For example, an ArrayList for integers:
ArrayList<Integer> listName = new ArrayList<>();
Pay attention:
- to the braces < > are used to define the data type of the elements;
- ArrayList accepts only object-oriented data types;
- the primitive data types are not allowed (use wrapper classes instead).
The most important methods which can be applied to ArrayLists are:
get
(returns the element at the specified position in this list)add
(inserts the specified element to the end of this list or at the specified position)addAll
(appends all elements of the specified collection to the end of this list)remove
(removes the element at the specified position in this list)contains
(checks if this list contains the specified element)size
(returns the number of elements in this list)indexOf
(returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element)
Useful link: Other useful methods of ArrayList class can be found here here.
Example:
import java.util.ArrayList; public class Test { public static void main(String[] args) { // compose an ArrayList ArrayList<Integer> list1 = new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3); // another option to compose an ArrayList ArrayList<Integer> list2 = new ArrayList<>(); list2.addAll(java.util.Arrays.asList(1,2,3)); // 1, 2, 3 // add an element into the ArrayList list1.add(2, 54); // 1, 2, 54, 3 // unlike arrays, ArrayLists can be printed without a loop System.out.println(list1); //ArrayLists can also be printed using a loop for (int i = 0; i < list2.size(); i++) System.out.print(list2.get(i)+" "); // invoke our method reverse System.out.println(reverse(list1)); } //create a method that reverses an ArrayList public static ArrayList<Integer> reverse(ArrayList<Integer> list) { ArrayList<Integer> result = new ArrayList<>(); for (int i = list.size()-1; i >= 0; i--) { result.add(list.get(i)); } return result; } }
Chapter 4 |