The ArrayList class is a resizable array, which can be found in the java.util package.
The difference between a built-in array and an ArrayList in Java is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayListwhenever you want. The syntax is also slightly different.
Other advantages and disadvantages of ArrayList are:
Java ArrayList class can contain duplicate elements.
Java ArrayList class maintains insertion order.
Java ArrayList class is nonsynchronized.
Java ArrayList allows random access because the array works on an index basis.
In ArrayList, manipulation is a little bit slower than the LinkedList in Java because a lot of shifting needs to occur if any element is removed from the array list.
add() to add a new element, e.g. cars.add("Volvo");
get() to access an element, e.g. cars.get(0);
set() to modify an element, e.g. cars.set(0, "Opel");
remove() to remove an element, e.g. cars.remove(0);
clear() to remove all the elements, cars.clear();
size to find out how many elements, e.g. cars.size();
Example: CarList
import java.util.ArrayList;
public class CarList {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
The ouput:
[Volvo, BMW, Ford, Mazda]
To access an element in the ArrayList, use the get() method and refer to the index number.
Example:
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(23);
num.add(1);
num.add(15);
num.add(30);
System.out.println(num.get(1));
output:
1
To modify an element, use the set() method and refer to the index number.
Example:
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(23);
num.add(1);
num.add(15);
num.add(30);
num.set(1, 4);
System.out.println(num.get(1));
Output:
4
To remove an element, use the remove() method and refer to the index number.
Example:
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(23);
num.add(1);
num.add(15);
num.add(30);
num.remove(2);
System.out.println(num);
Output:
[23, 1, 30]
To remove all the elements in the ArrayList, use the clear() method:
Example:
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(23);
num.add(1);
num.add(15);
num.add(30);
num.clear();;
System.out.println(num);
Output:
[]
To find out how many elements an ArrayList have, use the size method.
Example:
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(23);
num.add(1);
num.add(15);
num.add(30);
System.out.println(num.size());
Output:
4
Example: CarList2
import java.util.ArrayList;
import java.util.Collections;
public class CarList2 {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars); // Sort cars
for (String i : cars) {
System.out.println(i);
}
}
}
The output:
BMW
Ford
Mazda
Volvo
Loop through the elements of an ArrayList with a for loop, and use the size() method to specify how many times the loop should run:
Example: IntList
import java.util.ArrayList;
public class IntList {
public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
for (int i : myNumbers) {
System.out.println(i);
}
}
}
The output:
10
15
20
25