ArrayList in Java
Overview of ArrayList
- Definition: ArrayList is a class in the standard Java libraries designed to store any type of object. Unlike arrays, it can dynamically grow and shrink during program execution.
- Purpose: Similar to arrays, but with a key difference in flexibility regarding size.
Implementation
- Underlying Structure: ArrayList is implemented using a private hidden array.
- When the hidden array reaches its capacity, a new, larger array is allocated and elements are transferred.
Importing ArrayList
- To use ArrayList in your Java program, you must import it:
import java.util.ArrayList;
Creating an ArrayList
- Declaration Syntax:
ArrayList<BaseType> aList = new ArrayList<BaseType>();
- Initial Capacity: You can specify the initial capacity upon creation.
- Example:
java ArrayList<String> list = new ArrayList<String>(20);
- Example:
- Note: Specifying an initial capacity does not limit the ArrayList's growth.
Adding Elements
- Use the
addmethod to append elements:
list.add("something");
- There is an overloaded
addmethod that accepts an index and an element:
list.add(index, "something");
Size and Accessing Elements
- Use
sizemethod to check how many elements are present:
int howMany = list.size();
- Use
setmethod to modify an element andgetmethod to read an element:
list.set(index, "something else");
String thing = list.get(index);
- Clarification:
- Size: Number of elements currently in the ArrayList.
- Capacity: Maximum possible size, which is dynamic.
Example Code
- Basic Use:
public static void main(String[] args) {
ArrayList<Integer> myInts = new ArrayList<Integer>(25);
System.out.println("Size of myInts = " + myInts.size());
for (int k = 0; k < 10; k++) myInts.add(3 * k);
myInts.set(6, 44);
System.out.println("Size of myInts = " + myInts.size());
for (int k = 0; k < myInts.size(); k++) System.out.print(myInts.get(k) + ", ");
}
- Output:
Size of myInts = 0
Size of myInts = 10
0, 3, 6, 9, 12, 15, 44, 21, 24, 27
ArrayList Methods
- ArrayLists possess various methods for element manipulation which are typically more powerful than basic array manipulation (limited to length and indexing).
Copying an ArrayList
- Assignment Issue: Direct assignment will not copy elements but create a reference to the same ArrayList.
ArrayList<Integer> b = a; // Both refer to the same ArrayList
- Shallow Copy: Using
clone()will create a shallow copy.
ArrayList<Integer> b = a.clone();
- Deep Copy: Create a manual deep copy by copying elements individually.
Array vs ArrayList
When to Use Arrays:
- More efficient for fixed-size data.
- Requires simple index management.
- Cannot hold primitive types directly.
When to Use ArrayList:
- Flexibility in changing size during execution.
- Automatic tracking of the number of elements.
- Offers a rich set of methods for manipulation.
Comparison with Vector Class
- The
Vectorclass is similar toArrayListbut is older. ArrayList, introduced in Java 5, is the preferred option in most cases due to improvements and updates in its design.