ArrayList in java is a resizable array and it is the implementation of the List interface. There is no size limit of ArrayList class. The capacity of ArrayList will grow by adding new elements. Thus, you don't have to specify the size of ArrayList. Overall it is the main difference between java built in Array and ArrayList. We know that java Array size can not be modified but ArrayList size can be modified.
Additionally, there are lot of methods available to work with java ArrayList.
# Syntax:
ArrayList list = new ArrayList<>();
ArrayList fruits = new ArrayList();
//add elements
Note: T is the type of elements. You can't use primitive types. You have to use wrapper types like Integer, Float, String or objects. As primitive types aren't supported by ArrayList.
As I already mentioned that, there are plenty of methods available to work with java ArrayList.
# Methods of ArrayList:
Methods | Return types | Description |
---|---|---|
add(E element) | boolean | Appends the specified element to the end of this list. |
add(int index, E element) | void | Inserts the specified element at the specified position in this list. |
addAll(Collection c) | boolean | Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. |
addAll(int index, Collection c) | boolean | Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. |
get(int index) | E (element type) | Returns the element at the specified position in this list. |
isEmpty() | boolean | Returns true if this list contains no elements. |
iterator() | Iterator(E) | Returns an iterator over the elements in this list in proper sequence. |
remove(int index) | E | Removes the element at the specified position in this list. |
removeAll(Collection c) | boolean | Removes from this list all of its elements that are contained in the specified collection. |
removeIf(Predicate filter) | boolean | Removes all of the elements of this collection that satisfy the given predicate. |
set(int index, E element) | E | Replaces the element at the specified position in this list with the specified element. |
size() | int | Returns the number of elements in this list. |
trimToSize() | void | Trims the capacity of this ArrayList instance to be the list's current size. |
toArray() | Object | Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
contains() | boolean | Returns true if this list contains the specified element |
clone() | Object | Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.) |
equals() | boolean | Checks two lists are equal or not (returns true ir false) |
indexOf() | int | Returns the index of the first occurrence of the specified element in this list. |
indexOf() | int | Returns the index of the last occurrence of the specified element in this list. |
sort() | void | Sorts the list alphabetically or numerically in a specified order (Eescending or Descending) |
stream() | Stream | Returns a sequential Stream with this collection as its source. |
# Creating, adding elements to ArrayList
Creating java Generic ArrayList, add elements and print them
import java.util.ArrayList;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//print the elements
System.out.println(fruits);
}
}
If you run the above example application you see the following output in the console:
[Apple, Banana, Cherry, Guava, Plum]
# Iterate ArrayList elements
For iterating ArrayList elements we can use java for loop.
import java.util.ArrayList;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//iterate ArrayList elements
for(int i = 0; i <=fruits.size(); i++){
System.out.println(fruits.get(i));
}
}
}
If you run the above example you get the following output:
Apple
Banana
Cherry
Guava
Plum
# Iterating ArrayList elements using for each loop.
import java.util.ArrayList;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//iterating ArrayList elements
for(String f: fruits){
System.out.println(f);
}
}
}
// output:
/**
* Apple
* Banana
* Cherry
* Guava
* Plum
*/
# Iterating ArrayList using Iterator
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
//import all the classes
//import java.util.*;
public class Main{
public static void main (String[] args) {
//Creating ArrayList
ArrayList fruits = new ArrayList();
//add elements to this ArrayList
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//iterate using Iterator
Iterator list = fruits.iterator(); //getting Iterator
while(list.hasNext()){ //check if any elements exists or not
System.out.println(list.next()); //printing the elements
}
}
}
/**
* Apple
* Banana
* Cherry
* Guava
* Plum
*/
# Insert new elements to the ArrayList using add() method
We can use add() method to insert new elements to our existing ArrayList object.
Note: the add(element) insert new element at the end of the current list. And add(int index, element) insert new element at the specified index or position.
=> Using add(element):
import java.util.ArrayList;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//append a 6th number fruit
fruits.add("Blueberry");
//iterating ArrayList elements
for(String f: fruits){
System.out.println(f);
}
}
}
/**
* Apple
* Banana
* Cherry
* Guava
* Plum
* Banana
*/
=> Using add (int index, element) method:
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//iterating ArrayList elements
System.out.println("Before Changing");
for(String f: fruits){
System.out.println(f);
}
//change the fruits and then iterate
System.out.println("After changing");
fruits.set(0, "Blueberry");
//iterate now
for(String f: fruits){
System.out.println(f);
}
}
}
If you run the above program, you will get the following output in the console. You can see, the first fruits element has been updated.
Before Changing
Apple
Banana
Cherry
Guava
Plum
After changing
Blueberry
Banana
Cherry
Guava
Plum
Note: The add(int index, element) and set(int index, element) can be used interchangeably.
# Change an ArrayList element using set() method
import java.util.ArrayList;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//iterating ArrayList elements
System.out.println("Before Changing");
for(String f: fruits){
System.out.println(f);
}
//change the fruits and then iterate
System.out.println("After changing");
fruits.set(0, "Blueberry");
//iterate now
for(String f: fruits){
System.out.println(f);
}
}
}
If you run the above program, you will get the following output in the console. You can see, the first fruits element has been updated.
Before Changing
Apple
Banana
Cherry
Guava
Plum
After changing
Blueberry
Banana
Cherry
Guava
Plum
Note: The add(int index, element) and set(int index, element) can be used interchangeably.
# Remove an element from ArrayList
To remove an element, use the remove(int index). Don't forget to specified the index number.
Like:
import java.util.ArrayList;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add elements using add method
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Guava");
fruits.add("Plum");
//iterate ArrayList
System.out.println("Before Removing elements");
for(String f: fruits){
System.out.println(f);
}
//
System.out.println("After removing the first element");
fruits.remove(0);
//iterate now
for(String f: fruits){
System.out.println(f);
}
}
}
//output
Before Removing elements
Apple
Banana
Cherry
Guava
Plum
After removing the first element
Banana
Cherry
Guava
Plum
Note: It is possible to use remove(element) to remove a particular element based on their name instead of position.
# Sort the ArrayList elements using Collections.sort() method
Using Collections.sort() method we can easily sort the ArrayList in java. Sorting can be possible alphabetically (Strings) or numerically (numbers).
=> Sorting String elements: (Alphabetically)
import java.util.ArrayList;
import java.util.Collections;
public class Main{
public static void main (String[] args){
ArrayList fruits = new ArrayList();
//add new elements
fruits.add("Plum");
fruits.add("Cherry");
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Guava");
//sory fruits ArrayList
Collections.sort(fruits);
//iterating ArrayList elements
for(String f: fruits){
System.out.println(f);
}
}
}
// output:
/**
* Apple
* Banana
* Cherry
* Guava
* Plum
*/
=> Sorting numbers (numerically)
import java.util.ArrayList;
import java.util.Collections;
public class Main{
public static void main (String[] args){
ArrayList numbers = new ArrayList();
//add new elements
fruits.add(5);
fruits.add(3);
fruits.add(1);
fruits.add(4);
fruits.add(2);
//sort numbers ArrayList
Collections.sort(fruits);
//iterating ArrayList
for(String number: numbers){
System.out.println(number);
}
}
}
// output:
/**
* 1
* 2
* 3
* 4
* 5
*/