Courses / Java Collections Framework
1. ArrayList Class
A resizable array implementation of the List interface, allowing dynamic array size.
How It Works
Think of an ArrayList as a container that can stretch or shrink based on your needs, similar to a rubber band that can hold more or fewer items without breaking.
You initialize an ArrayList to create a dynamic array.
You can add elements to the ArrayList using the `add` method.
The ArrayList automatically resizes to accommodate new elements.
You can retrieve elements using their index with the `get` method.
See It
The code creates an ArrayList of strings, adds "Apple" and "Banana", and prints the first element. The output is "Apple" because it is at index 0. Try adding another fruit and printing the entire list.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(0)); // Output: Apple
}
}Use It
Storing a list of user inputs in a chat application.
import java.util.ArrayList;
public class Chat {
private ArrayList<String> messages = new ArrayList<>();
public void addMessage(String message) {
messages.add(message);
}
public String getMessage(int index) {
return messages.get(index);
}
}ArrayLists allow for easy management of messages as users send and receive them, growing as needed.
Break It
What will this print?
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(2)); // IndexOutOfBoundsException
}
}The code attempts to access index 2, but the ArrayList only has two elements at indices 0 and 1, causing an exception.
Change the index in the `get` method to a valid index, such as 1.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(1)); // Output: Banana
}
}Level Up
Using Generics with ArrayList: Generics allow you to create type-safe collections, ensuring only specified types are added to the ArrayList.
ArrayList vs LinkedList: Understanding the differences helps in choosing the right data structure for performance based on usage patterns.
Synchronized ArrayList: This allows for thread-safe operations on an ArrayList, which is crucial in multi-threaded applications.
Prove It
Create an ArrayList to store and retrieve the names of your favorite movies.
Java
ArrayList Class
An ArrayList is a flexible array that can grow and shrink in size as needed.
"I want to store a list of items without worrying about how many I'll need."
The ArrayList class in Java provides a resizable array implementation of the List interface, allowing dynamic management of elements. It can grow or shrink as elements are added or removed.
"I want to store a list of items without worrying about how many I'll need."
The ArrayList class in Java provides a resizable array implementation of the List interface, allowing dynamic management of elements. It can grow or shrink as elements are added or removed.