Java ArrayList
The ArrayList class is a dynamically resizable array, differing from regular arrays in that it has no fixed-size limit; elements can be added or removed.
ArrayList inherits from AbstractList and implements the List interface.
The ArrayList class is located in the java.util package and needs to be imported before use. The syntax is as follows:
import java.util.ArrayList; // Import the ArrayList class
ArrayList<E> objectName = new ArrayList<>(); // Initialization
- E: Generic data type, used to set the data type of objectName, only reference data types are allowed.
- objectName: Object name.
ArrayList is an array queue that provides functionalities for adding, removing, modifying, and traversing elements.
Adding Elements
The ArrayList class provides useful methods; elements can be added to an ArrayList using the add() method:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Taobao, Weibo]
Accessing Elements
Elements in an ArrayList can be accessed using the get() method:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites.get(1)); // Access the second element
}
}
Note: Array indices start from 0.
The output of the above example is:
tutorialpro
Modifying Elements
To modify elements in an ArrayList, use the set() method:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
sites.set(2, "Wiki"); // The first parameter is the index position, the second is the value to be modified
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Wiki, Weibo]
Deleting Elements
To delete elements in an ArrayList, use the remove() method:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
sites.remove(2); // Remove the third element
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Weibo]
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
sites.remove(3); // Removes the fourth element
System.out.println(sites);
}
}
The output of the above example is:
[Google, tutorialpro, Taobao]
Calculating Size
To calculate the number of elements in an ArrayList, you can use the size()
method:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites.size());
}
}
The output of the above example is:
4
Iterating Over an ArrayList
We can use a for
loop to iterate over the elements in an ArrayList:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
for (int i = 0; i < sites.size(); i++) {
System.out.println(sites.get(i));
}
}
}
The output of the above example is:
Google
tutorialpro
Taobao
Weibo
You can also use a for-each loop to iterate over the elements:
Example
import java.util.ArrayList;
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("tutorialpro");
sites.add("Taobao");
sites.add("Weibo");
for (String i : sites) {
System.out.println(i);
}
}
}
The output of the above example is:
Google
tutorialpro
Taobao
Weibo
Other Reference Types
The elements in an ArrayList are actually objects. In the above examples, the elements are of type String.
If we want to store other types, we need to use the wrapper classes of the primitive types.
Here is a table of primitive types and their corresponding wrapper classes:
Primitive Type | Reference Type |
---|---|
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
Additionally, BigInteger and BigDecimal are used for high-precision operations. BigInteger supports arbitrary-precision integers and is also a reference type, but they do not have corresponding primitive types.
ArrayList<Integer> li = new ArrayList<>(); // Stores integer elements
ArrayList<Character> li = new ArrayList<>(); // Store character elements
The following example uses ArrayList to store numbers (using Integer type):
Example
import java.util.ArrayList;
public class tutorialproTest {
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 of the above example is:
10
15
20
25
Sorting ArrayList
The Collections class is also a very useful class located in the java.util package, providing the sort() method to sort lists of characters or numbers.
The following example sorts letters:
Example
import java.util.ArrayList;
import java.util.Collections; // Import the Collections class
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Taobao");
sites.add("Wiki");
sites.add("tutorialpro");
sites.add("Weibo");
sites.add("Google");
Collections.sort(sites); // Sort alphabetically
for (String i : sites) {
System.out.println(i);
}
}
}
The output of the above example is:
Google
tutorialpro
Taobao
Weibo
Wiki
The following example sorts numbers:
Example
import java.util.ArrayList;
import java.util.Collections; // Import the Collections class
public class tutorialproTest {
public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(33);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(34);
myNumbers.add(8);
myNumbers.add(12);
Collections.sort(myNumbers); // Sort numerically
for (int i : myNumbers) {
System.out.println(i);
}
}
}
The output of the above example is:
8
12
15
20
33
34
Java ArrayList Methods
The commonly used methods in Java ArrayList are listed below:
Method | Description |
---|---|
add() | Inserts the specified element into the specified position in the arraylist |
addAll() | Adds all elements of the collection to the arraylist |
clear() | Removes all elements from the arraylist |
clone() | Clones an arraylist |
contains() | Checks if an element is in the arraylist |
get() | Retrieves an element from the arraylist by index |
indexOf() | Returns the index of an element in the arraylist |
removeAll() | Removes all elements from the arraylist that are present in the specified collection |
remove() | Removes a single element from the arraylist |
size() | Returns the number of elements in the arraylist |
isEmpty() | Checks if the arraylist is empty |
subList() | Extracts a portion of the arraylist |
set() | Replaces an element at a specified index in the arraylist |
sort() | Sorts the elements of the arraylist |
toArray() | Converts the arraylist to an array |
toString() | Converts the arraylist to a string |
ensureCapacity | Sets the capacity of the arraylist to a specified size |
lastIndexOf() | Returns the last occurrence of a specified element in the arraylist |
retainAll() | Retains only the elements in the arraylist that are also in the specified collection |
containsAll() | Checks if the arraylist contains all elements from the specified collection |
trimToSize() | Adjusts the capacity of the arraylist to the number of elements |
removeRange() | Removes elements between specified indices in the arraylist |
replaceAll() | Replaces each element in the arraylist with the result of the given operation |
removeIf() | Removes all elements from the arraylist that satisfy a given condition |
forEach() | Iterates over each element in the arraylist and performs a specified action |
For more API methods, see: https://www.tutorialpro.org/manual/jdk11api/java.base/java/util/ArrayList.html