ArrayList is part of the java.util
package and is a resizable array that allows you to store elements. Unlike arrays, which have a fixed size, an ArrayList dynamically adjusts its size as elements are added or removed.
Here’s a basic overview and implementation of ArrayList in Java:
1. Import the ArrayList Class
To use ArrayList, you need to import it from the java.util
package:
import java.util.ArrayList;
2. Creating an ArrayList
You can create an ArrayList in the following ways:
Example 1: Creating an ArrayList without any initial elements:
ArrayList<String> list = new ArrayList<>();
This creates an ArrayList of String
type. The <>
syntax is called the diamond operator.
Example 2: Creating an ArrayList with an initial capacity:
ArrayList<Integer> list = new ArrayList<>(10);
This initializes an ArrayList with an initial capacity of 10.
3. Adding Elements to the ArrayList
You can add elements to an ArrayList using the add()
method.
Example:
list.add("Apple");
list.add("Banana");
list.add("Cherry");
This adds the elements "Apple"
, "Banana"
, and "Cherry"
to the ArrayList.
4. Accessing Elements
You can access the elements of an ArrayList using the get()
method, which takes an index as an argument.
Example:
String item = list.get(0); // Gets the first element, "Apple"
5. Modifying Elements
To modify an element, you can use the set()
method.
Example:
list.set(1, "Blueberry"); // Changes "Banana" to "Blueberry"
6. Removing Elements
You can remove elements using the remove()
method.
Example:
list.remove("Apple"); // Removes the element "Apple"
list.remove(0); // Removes the element at index 0 ("Blueberry")
7. Size of the ArrayList
To find the number of elements in the ArrayList, use the size()
method.
Example:
int size = list.size(); // Returns the size of the list
8. Iterating over an ArrayList
You can iterate over the elements of an ArrayList using several methods, such as a for
loop or an enhanced for-each
loop.
Example:
for (String item : list) {
System.out.println(item); // Prints each element
}
Alternatively, you can use the forEach
method (Java 8 and above):
list.forEach(item -> System.out.println(item));
9. Clearing the ArrayList
To remove all elements from the ArrayList, use the clear()
method.
Example:
list.clear(); // Removes all elements
10. Checking if the ArrayList contains an element
Use the contains()
method to check if an element exists in the ArrayList.
Example:
boolean contains = list.contains("Cherry"); // Returns true if "Cherry" is in the list
Full Example Code
Here’s an example that demonstrates all of the above methods:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList of String type
ArrayList<String> list = new ArrayList<>();
// Add elements to the ArrayList
list.add("Apple");
list.add("Banana");
list.add("Cherry");
// Accessing an element
System.out.println("Element at index 0: " + list.get(0)); // Output: Apple
// Modifying an element
list.set(1, "Blueberry");
// Removing an element by value
list.remove("Apple");
// Removing an element by index
list.remove(0); // Removes "Blueberry" (now at index 0)
// Iterating over the ArrayList
System.out.println("Updated ArrayList:");
for (String item : list) {
System.out.println(item);
}
// Checking if the ArrayList contains a specific element
boolean containsCherry = list.contains("Cherry");
System.out.println("Contains 'Cherry': " + containsCherry); // Output: true
// Getting the size of the ArrayList
System.out.println("Size of the list: " + list.size()); // Output: 1
// Clearing the ArrayList
list.clear();
System.out.println("Size after clearing: " + list.size()); // Output: 0
}
}
Output:
Element at index 0: Apple
Updated ArrayList:
Cherry
Contains 'Cherry': true
Size of the list: 1
Size after clearing: 0
Key Points:
- ArrayList is a dynamic array that grows and shrinks in size.
- It allows duplicate elements and maintains the order of insertion.
- It is not synchronized, which means if you’re working in a multi-threaded environment, you might need to consider using a
Vector
or synchronizing the list.
Performance Considerations:
- ArrayList provides fast access to elements by index (
O(1)
complexity), but removing elements from the middle (O(n)
complexity) can be slow due to shifting elements.