Understanding and Using Java ArrayList: Features and Operations
Characteristics of Arrays
- Random Access: Elements can be accessed in O(1) time complexity using their index without needing to traverse the entire structure.
- Contiguous Storage: Arrays are physically and logically contiguous in memory.
- Fixed Size: Once initialized, an array's length is immutable. Expanding an array requires allocating a new, larger array and copying the contents. In Java, arrays are objects.
ArrayListacts as a resizable array container that handles this expansion automatically.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(3);
ArrayList Overview
-
Key Properties
ArrayListis a concrete implementation of theListinterface.- It internally uses an
Object[]array (default initial capacity is 10) to store elements, and it growss dynamically as needed. - Read operations are fast due to array indexing, but insertions and deletions in the middle are slower because they require shifting elements.
- It is not synchronized (not thread-safe).
-
Common Methods and Usage
-
add(E element): Appends the specified element to the end of the list.import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); System.out.println(items); System.out.println(items.size()); System.out.println(items.get(1)); } } -
add(int index, E element): Inserts the element at the specified position, shifting subsequent elements to the right.public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); System.out.println(items); items.add(1, "7"); System.out.println(items); } } -
set(int index, E element): Replaces the element at the specified position and returns the element previously at that position.public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); System.out.println(items); String oldValue = items.set(1, "6"); System.out.println(items); System.out.println(oldValue); } } -
clear(): Removes all elements from the list. -
isEmpty(): Returnstrueif the list contains no elements.public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); System.out.println(items); items.clear(); System.out.println(items.isEmpty()); System.out.println(items); } } -
contains(Object o): Returnstrueif the list contaisn the specified element.public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); System.out.println(items.contains("3")); System.out.println(items.contains("6")); } } -
remove(int index): Removes the element at the specified position, shifts subsequent elements left, and returns the removed element.public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); items.add("56"); System.out.println(items); String removed = items.remove(1); System.out.println(removed); System.out.println(items); } } -
remove(Object o): Removes the first occurrence of the specified element. Returnstrueif the list contained the element.public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); items.add("34"); items.add("56"); System.out.println(items); boolean isRemoved = items.remove("34"); System.out.println(isRemoved); System.out.println(items); } } -
iterator(): Returns anIteratorto traverse the elements in order.import java.util.ArrayList; import java.util.Iterator; public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); Iterator<String> iter = items.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } } } -
Standard
forloop traversal:public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); for (int i = 0; i < items.size(); i++) { System.out.println(items.get(i)); } } } -
Enhanced
forloop (for-each) traversal:public class Demo { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("12"); items.add("34"); for (String value : items) { System.out.println(value); } } }
-