In Java, an ArrayList is a part of the java.util
package and provides a dynamic array for storing elements. Unlike regular arrays, which have a fixed size, an ArrayList can grow and shrink dynamically as elements are added or removed. This makes it a versatile option for managing collections of data where the size is unknown or likely to change.
Key Features of an ArrayList:
- Dynamic Size: Automatically resizes when elements are added or removed.
- Indexed Access: Elements can be accessed directly via their index (like arrays).
- Generics Support: Can specify the type of elements it contains (e.g.,
ArrayList<String>
).
Syntax:
ArrayList<Type> arrayListName = new ArrayList<Type>();
- Type: The type of elements stored in the list (e.g.,
Integer
,String
,CustomClass
). - arrayListName: The name of the ArrayList.
Common Operations:
- Creating an ArrayList:
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); } }
- Adding Elements:
Use theadd()
method to append elements to the list.
names.add("Alice"); names.add("Bob");
- Accessing Elements:
Use theget()
method to access an element by its index.
String firstPerson = names.get(0); // "Alice"
- Modifying Elements:
Theset()
method updates an element at a specific index.
names.set(1, "Charlie"); // Replaces "Bob" with "Charlie"
- Removing Elements:
Theremove()
method deletes an element by its index or by the element itself.
names.remove(0); // Removes the element at index 0 ("Alice") names.remove("Charlie"); // Removes "Charlie" by value
- Checking Size:
Usesize()
to find out how many elements are in the ArrayList.
int size = names.size(); // Size of the ArrayList
- Looping Through an ArrayList:
You can iterate over an ArrayList using afor
loop or an enhancedfor-each
loop.
for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i)); } for (String name : names) { System.out.println(name); }
- Checking if an Element Exists:
Usecontains()
to check if an element exists in the list.
boolean hasAlice = names.contains("Alice"); // true or false
Example: Managing a List of Numbers
import java.util.ArrayList; public class Main { public static void main(String[] args) { // Create an ArrayList to store integers ArrayList<Integer> numbers = new ArrayList<Integer>(); // Add elements to the ArrayList numbers.add(10); numbers.add(20); numbers.add(30); // Print all elements for (Integer number : numbers) { System.out.println(number); } // Remove an element numbers.remove(1); // Removes element at index 1 (20) // Print the updated ArrayList System.out.println("Updated List: " + numbers); } }
Important Methods in ArrayList
:
Method | Description |
---|---|
add(element) | Adds an element to the end of the list |
add(index, element) | Inserts an element at the specified index |
get(index) | Returns the element at the specified index |
set(index, element) | Replaces the element at the specified index |
remove(index) | Removes the element at the specified index |
remove(Object) | Removes the first occurrence of the specified element |
size() | Returns the number of elements in the list |
contains(Object) | Checks if the list contains the specified element |
clear() | Removes all elements from the list |
isEmpty() | Checks if the list is empty |
ArrayList vs Array:
- Array: Fixed in size, can store both primitives and objects.
- ArrayList: Dynamic in size, can only store objects (not primitives). However, Java automatically handles autoboxing for primitive types (e.g.,
int
toInteger
).
Performance Considerations:
- Time Complexity:
- Accessing an element by index: O(1).
- Adding an element (at the end): O(1) (amortized).
- Removing or adding elements in the middle: O(n) (since elements may need to be shifted).
Conclusion:
ArrayList
is a flexible and powerful data structure in Java, allowing for dynamic resizing and easy management of elements. It is particularly useful when you need a resizable array and provides many built-in methods to make operations easier and more efficient.