ArrayList is a resizable array whose elements can be added or removed.
ArrayList Class
Import Statement
The ArrayList
class is in the java.util
package. Java doesn’t import this automatically, so we need to import it manually.
import java.util.*; // imports all classes
import java.util.ArrayList; // imports ArrayList class only
The import statement appears before the Main class code block.
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
}
}
Constructor
There are 3 ArrayList constructor overloads.
public ArrayList()
public ArrayList(int initialCapacity)
public ArrayList(Collection<? extends E> c)
The default constructor creates an empty ArrayList with the default capacity of ten.
The second constructor creates an empty ArrayList with the capacity specified in the input parameter.
The third constructor creates an ArrayList containing the elements of the specified collection.
Declaration
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/08/c25-1-1.png?w=600)
Data_Type is the data type of the elements in the list. It is written inside a pair of angle brackets <>
. ArrayList elements are objects, so Data_Type must be a reference type.
Parameter N
represents ArrayList capacity which is the memory size allocated to store the elements. It is greater than or equal to the number of elements.
Collection can be another ArrayList or any other collections.
Example
ArrayList<String> names = new ArrayList<String>();
ArrayList<Double> gpa = new ArrayList<Double>(3);
ArrayList<Double> moreGPA = new ArrayList<Double>(gpa);
Line 1 – Default constructor is invoked to create an empty ArrayList of String type. Variable names
refers to this object.
Line 2 – Second constructor is invoked to create an empty ArrayList of Double type. Variable gpa
refers to this object. We use the wrapper class Double
because ArrayList data type must be a reference type.
Line 3 – Third constructor is invoked to create an ArrayList containing the elements of ArrayList gpa
. Variable moreGPA
refers to this object.
Methods
This section covers only some of the methods in the ArrayList class. Refer to the Java SE documentation online for a complete list.
add
public boolean add (Element e)
This method appends a new element to the end of a list. If a list is empty, it adds to index position zero. The method returns true
if the content of a list is changed after the method completes. Otherwise, it returns false
.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
if (product.add("Earphones") == false) {
System.out.println("Oops. Something went wrong.");
}
Line 2 – Used as standalone statement. This adds a new element “Earbuds”.
Line 4 – Used in a conditional statement. This adds a new element “Earphones” after “Earbuds”.
public void add (int index, Element e)
This method inserts a new element to the specified index position. The element currently at the index position and any subsequent elements are shifted to the right to make room.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
product.add("Earphones");
System.out.println(">> Before:");
for (String p: product) {
System.out.println(p);
}
product.add(1, "Headphones");
System.out.println(">> After:");
for (String p: product) {
System.out.println(p);
}
Lines 2 and 3 – Adds two elements to the list.
Line 10
– Inserts a new element at index position 1.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/08/c25-2.png?w=375)
remove
public boolean remove (Element e)
This method removes the first occurrence of the element from a list. If the element does not exist, the list does not change. The method returns true
if the content of a list is changed after the method completes. Otherwise, it returns false
.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
product.add("Headphones");
product.add("cellphone");
product.add("Earphones");
product.add("megaphone");
product.add("cellphone");
product.remove("cellphone");
if (product.remove("cellphone") == false) {
System.out.println("Oops. Something went wrong.");
}
Line 9 – Used as standalone statement. Removes the first occurrence of “cellphone” which is in the index position 2.
Line 11 – Used in the conditional statement. Removes “cellphone” in the last index position.
public Element remove (int index)
This method removes the element at the specified index position. The element currently at the index position and any subsequent elements are shifted to the left. The method returns the removed element.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
product.add("Headphones");
product.add("cellphone");
product.add("Earphones");
product.add("megaphone");
product.add("cellphone");
product.remove(5);
System.out.println(product.remove(2));
Line 9 – Used as standalone. Removes the element at the index position 5.
Line 11 – Used in a print method. Removes the element at the index position 2 and returns the removed element. The value of this element is used by the print method.
get
public Element get (int index)
The method returns the element at the specified index position.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
product.add("Headphones");
product.add("Earphones");
product.add("megaphone");
product.add("cellphone");
System.out.println(product.get(3));
Line 8 – Returns the element at the index position 3. The value of this element is used by the print method.
set
public Element set (int index, Element e)
This method replaces the element at the specified index position with a new element. The method returns the original element that has been replaced.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
product.add("Headphones");
product.add("cellphone");
product.add("Earphones");
product.add("megaphone");
product.set(4, "Bluetooth speaker");
System.out.println(product.set(2, "Mobile phone"));
Line 8 – Used as standalone. Replaces the element at the index position 4 with a new element “Bluetooth speaker”.
Line 10
– Used in a print method. Replaces the element at the index position 2 with a new element “Mobile phone”. The replaced element “cellphone” is returned to be used by the print method.
indexOf
public int indexOf (Element e)
This method returns the index of the first occurrence of the specified element. If the element does not exist in a list, -1
is returned.
lastIndexOf
public int lastIndexOf (Element e)
This method returns the index of the last occurrence of the specified element. If the element does not exist in a list, -1
is returned.
contains
public boolean contains (Element e)
This method returns true
if a list contains the specified element. Otherwise, false
.
Example
ArrayList<String> product = new ArrayList<String>();
product.add("Earbuds");
product.add("Headphones");
product.add("cellphone");
product.add("Earphones");
product.add("megaphone");
product.add("cellphone");
if (product.contains("telephone")) {
System.out.println("telephone exists");
}
System.out.println(product.indexOf("cellphone")); // 2
System.out.println(product.lastIndexOf("cellphone")); // 5
Line 9 – Checks whether “telephone” exists in the list.
Line 13 – Returns the first occurrence of “cellphone” which is index 2.
Line 15 – Returns the last occurrence of “cellphone” which is index 5.
size
public int size ()
This method returns the number of elements in a list.
isEmpty
public boolean isEmpty ()
This method returns true
if a list has no elements. Otherwise, false
.
clear
public void clear ()
This method removes all elements from a list.
2022 Copyright