ArrayList in Java
Introduction:
ArrayList is a part of the Java Collection Framework that provides us with dynamic arrays. It's a resizable
array implementation of the List interface. Unlike arrays, ArrayLists can dynamically grow and shrink in
size as elements are added or removed. It provides us with several utility methods to manipulate the
elements efficiently.
Declaration and Initialization:
To use an ArrayList in Java, you need to import the java.util package at the beginning of your code.
Here's how you declare and initialize an ArrayList:
import java.util.ArrayList;
// Declaration
ArrayList<String> myArrayList;
// Initialization
myArrayList = new ArrayList<String>(); // Creating an empty ArrayList
// You can also create and initialize with elements
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Basic Operations:
1. Adding Elements:
.add(element) - Appends the specified element to the end of the ArrayList.
myArrayList.add("Apple");
myArrayList.add("Banana");
myArrayList.add("Orange");
2. Accessing Elements:
.get(index) - Returns the element at the specified index in the ArrayList.
String fruit = myArrayList.get(1); // Retrieves the element at index 1 (Banana)
3. Removing Elements:
.remove(index) - Removes the element at the specified index from the ArrayList.
.remove(object) - Removes the first occurrence of the specified element from the ArrayList.
myArrayList.remove(2); // Removes the element at index 2 (Orange)
myArrayList.remove("Apple"); // Removes the element "Apple" from the ArrayList
4. Size and Checking:
.size() - Returns the number of elements in the ArrayList.
.isEmpty() - Checks if the ArrayList is empty or not.
int size = myArrayList.size(); // Gets the size of the
ArrayList boolean isEmpty = myArrayList.isEmpty(); // Checks if the ArrayList is empty
5. Iterating through ArrayList:
Using enhanced for loop
Using Iterator
// Using enhanced for loop
for (String fruit : myArrayList)
System.out.println(fruit);
// Using Iterator
Iterator<String> iterator = myArrayList.iterator();
while (iterator.hasNext())
System.out.println(iterator.next());
}
Here are some key methods along with examples:
1. Adding Elements:
add(E element)
Adds the specified element at the end of the ArrayList.
Example:
rrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
add(int index, E element)
Inserts the specified element at the specified position in the ArrayList.
Example:
fruits.add(1, "Orange"); // Adds "Orange" at index 1
2. Accessing Elements:
get(int index)
Returns the element at the specified index in the ArrayList.
Example:
String fruit = fruits.get(0); // Retrieves the element at index 0 (Apple)
3. Removing Elements:
remove(int index)
Removes the element at the specified index from the ArrayList.
Example:
fruits.remove(1); // Removes the element at index 1 (Banana)
remove(Object obj)
Removes the first occurrence of the specified element from the ArrayList.
Example:
fruits.remove("Apple"); // Removes the element "Apple" from the ArrayList
4. Checking and Retrieval:
size()
Returns the number of elements in the ArrayList.
Example:
int size = fruits.size(); // Gets the size of the ArrayList
isEmpty()
Checks if the ArrayList is empty or not.
Example:
boolean isEmpty = fruits.isEmpty(); // Checks if the ArrayList is empty
5. Iterating through ArrayList:
Using Enhanced For Loop:
Example:
for (String fruit : fruits) { System.out.println(fruit); }
Using Iterator:
Example:
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext())
{ System.out.println(iterator.next()); }
6. Conversion to Array:
toArray()
Converts the ArrayList to an array.
Example:
String[] fruitsArray = fruits.toArray(new String[0]);
7. Clearing the ArrayList:
clear()
Removes all elements from the ArrayList.
Example:
fruits.clear(); // Clears all elements from the ArrayList
These methods provide versatile functionality for handling ArrayLists in Java. Is there any specific
method or functionality you'd like to explore further?
Advantages of ArrayList:
Dynamic Size: ArrayLists can grow and shrink dynamically, unlike arrays.
Generics: Type safety is provided as ArrayLists can be parameterized with types.
Various Methods: It offers numerous methods for manipulation like adding, removing, accessing
elements, etc.
Disadvantages of ArrayList:
Slower Performance: Compared to arrays, ArrayLists can be slower due to their dynamic resizing nature.
Memory Overhead: ArrayLists consume more memory than arrays as they store additional information
for flexibility.