Java ArrayList Methods With Examples
In this post we cover the most common Java ArrayList methods with code examples for each method.
The ArrayList
class is a resizable array. We typically use ArrayList
when we don’t know the size of the list beforehand.
The ArrayList
class is in the java.util
package.
How to Create an ArrayList
To create an ArrayList and add items to it, we instantiate an ArrayList
object with a specified type, such as String
or Integer
.
Note the add()
method adds an item to the next position in the ArrayList. See the next example to see how to add an item to a specific index.
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
System.out.println(fruitBasket);
}
}
Output:
[Apple, Banana, Grapes, Orange]
How to Add an Item to a Specific Position
To add an item to a specified position, we have to provide the desired index to the add()
method.
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
fruitBasket.add(2, "Melon");
System.out.println(fruitBasket);
}
}
Output:
[Apple, Banana, Melon, Grapes, Orange]
How to Access an Item in an ArrayList
The get()
method retrieves an item. We have to provided the index number to the get()
method.
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
System.out.println(fruitBasket.get(1));
}
}
Banana
How to Remove an Item From an ArrayList
To remove an item, we use the remove()
method. We have to provide the index number.
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
fruitBasket.remove(0);
System.out.println(fruitBasket);
}
}
Output:
[Banana, Grapes, Orange]
How to Remove All Items From an ArrayList
The clear()
method removes all items from an ArrayList
.
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
fruitBasket.clear();
System.out.println(fruitBasket);
}
}
[]
How to Get the Size of an ArrayList
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
System.out.println(fruitBasket.size());
}
}
Output:
4
How to Loop Through an ArrayList
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
for (int i = 0; i < fruitBasket.size(); i++) {
System.out.println(fruitBasket.get(i));
}
}
}
Output:
Apple
Banana
Grapes
Orange
Looping Through an ArrayList With for-each
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
for (String i : fruitBasket) {
System.out.println(i);
}
}
}
Output:
Apple
Banana
Grapes
Orange
ArrayList Example With Numbers
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
for (int i : numbers) {
System.out.println(i);
}
}
}
Output:
10
20
30
40
How to Replace an Item in an ArrayList
The replace()
method replaces an item with another provided item. We have to specify the index of the item we want to replace.
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
fruitBasket.set(3, "Peach")
for (String i : fruitBasket) {
System.out.println(i);
}
}
}
Output:
#before replace
[Apple, Banana, Grapes, Orange]
#after replace
[Apple, Banana, Grapes, Peach]
How to Shuffle Items in an ArrayList
The shuffle()
method is in the Collections
class and is used to do a random shuffle of the ArrayList items.
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
System.out.println(fruitBasket);
Collections.shuffle(fruitBasket);
System.out.println(fruitBasket);
}
}
Output:
#before shuffle
[Apple, Banana, Grapes, Orange]
#after shuffle
[Grapes, Orange, Banana, Apple]
How to Sort Items in an ArrayList
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Orange");
fruitBasket.add("Apple");
fruitBasket.add("Grapes");
fruitBasket.add("Banana");
System.out.println(fruitBasket);
Collections.sort(fruitBasket);
System.out.println(fruitBasket);
}
}
Output:
#before sort
[Orange, Apple, Grapes, Banana]
#after sort
[Apple, Banana, Grapes, Orange]
How to Reverse the Items on an ArrayList
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
System.out.println(fruitBasket);
Collections.reverse(fruitBasket);
System.out.println(fruitBasket);
}
}
Output:
#original list
[Apple, Banana, Grapes, Orange]
#after reverse
[Orange, Grapes, Banana, Apple]
How to Convert an ArrayList to Array
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
String[] fruitArray = new String[fruitBasket.size()];
fruitBasket.toArray(fruitArray);
for (String i : fruitArray) {
System.out.println(i);
}
}
}
Output:
Apple
Banana
Grapes
Orange
How to Swap Two Items in an ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket = new ArrayList<>();
fruitBasket.add("Apple");
fruitBasket.add("Banana");
fruitBasket.add("Grapes");
fruitBasket.add("Orange");
System.out.println(fruitBasket1);
Collections.swap(fruitBasket, 1, 3);
System.out.println(fruitBasket);
}
}
Output:
#before swap
[Apple, Banana, Grapes, Orange]
#after swap
[Apple, Orange, Grapes, Banana]
How to Join Two ArrayLists
import java.util.ArrayList;
public class ArrayListTutorial {
public static void main(String[] args) {
ArrayList<String> fruitBasket1 = new ArrayList<>();
ArrayList<String> fruitBasket2 = new ArrayList<>();
fruitBasket1.add("Apple");
fruitBasket1.add("Banana");
fruitBasket1.add("Grapes");
fruitBasket1.add("Orange");
fruitBasket2.add("Melon");
fruitBasket2.add("Strawberries");
fruitBasket1.addAll(fruitBasket2);
System.out.println(fruitBasket1);
}
}
Output:
[Apple, Banana, Grapes, Orange, Melon, Strawberries]
Conclusion
In this post you saw different examples of the most common operations on Java ArrayLists.