What a List is and why you need one
A List in Java is a container that holds multiple items in a specific order. Unlike an array, which has a fixed size, a List grows and shrinks as you add or remove items. This makes Lists far more practical for real work — you don't have to guess how many items you'll need ahead of time.
Java's List is part of the Collections Framework, a set of built-in tools for storing and organizing data. The most common List type is ArrayList, which you'll use in the vast majority of cases. When you create an ArrayList, you're creating an object that can hold as many items as your program needs, and you can add, remove, or access those items by their position.
Key Takeaways
- ArrayList is the most common List type in Java and automatically grows when you add items to it.
- You create a List by declaring its type, giving it a name, and using the new keyword followed by ArrayList with angle brackets specifying what kind of items it holds.
- Add items with the add() method, access them by position with get(), and remove them with the remove() method.
- Items in a List are numbered starting from 0, so the first item is at position 0, the second at position 1, and so on.
- You can loop through all items in a List using a for-each loop, which is simpler than counting positions yourself.
Creating your first ArrayList
To create an ArrayList, you write a line that tells Java three things: what type of items the list will hold, what you want to call the list, and that you want a new ArrayList. Here's the basic pattern:
ArrayList<String> names = new ArrayList<String>();
Break this down: ArrayList<String> means you're creating a list that holds text (String is Java's word for text). The angle brackets with String inside tell Java what kind of items belong in this list. names is the variable name — you choose this, and it's what you'll type whenever you want to work with this list. new ArrayList<String>() actually creates the list object in memory and is ready to hold items.
You can create Lists that hold different types of items. If you want a list of whole numbers, write ArrayList<Integer>. If you want a list of decimal numbers, write ArrayList<Double>. The type you put in the angle brackets must match what you plan to store.
Adding items to your List
Once you have a List, you add items to it using the add() method. Each time you call add(), the item goes to the end of the list:
names.add("Alice"); names.add("Bob"); names.add("Charlie");
After these three lines, your list contains three items in this order: Alice at position 0, Bob at position 1, and Charlie at position 2. If you add a fourth item, it automatically goes to position 3. You never have to tell Java how big the list should be — it handles that for you.
You can also insert an item at a specific position using add() with two arguments. The first argument is the position, and the second is the item:
names.add(1, "Diana");
This puts Diana at position 1, which pushes Bob and everyone after him one position forward. Now Bob is at position 2 instead of position 1.
Getting items out of your List
To retrieve an item from your list, use the get() method with the position number in parentheses. Remember that positions start at 0:
String first = names.get(0);
This line retrieves the item at position 0 (the first item) and stores it in a variable called first. If you want the third item, you'd write names.get(2). If you ask for a position that doesn't exist — like position 10 when your list only has 5 items — Java will throw an error and your program will crash, so be careful.
Before you get an item, you can check how many items are in your list using the size() method:
int count = names.size();
This tells you the total number of items. If size() returns 5, you know the valid positions are 0 through 4.
Removing items from your List
To remove an item, use the remove() method with the position of the item you want to delete:
names.remove(1);
This removes the item at position 1. Everything after it shifts forward one position. If your list had Alice, Bob, Charlie, and Diana, and you remove position 1 (Bob), now position 1 contains Charlie instead.
You can also remove an item by its value rather than its position. If you write names.remove("Bob"), Java will find Bob in the list and remove it. If the item appears multiple times, only the first occurrence is removed. If the item isn't in the list at all, nothing happens and no error is thrown.
Looping through all items in a List
The most common way to work with every item in a List is a for-each loop. This loop automatically goes through each item one at a time without you having to count positions:
for (String name : names) { System.out.println(name); }
This reads as: "For each String called name in the list called names, print that name." The loop runs once for each item in the list. On the first run, name is Alice. On the second run, name is Bob. And so on. You don't have to worry about positions or the size of the list — Java handles all of that.
If you do need to know the position of each item, you can use a traditional for loop instead:
for (int i = 0; i < names.size(); i++) { String name = names.get(i); System.out.println(i + ": " + name); }
This loop counts from 0 up to the size of the list, gets the item at each position, and prints both the position and the item. This approach is useful when you need to know where each item is.
Common mistakes and how to avoid them
The most frequent error is forgetting that positions start at 0. If your list has 5 items, the last one is at position 4, not position 5. Trying to get position 5 will crash your program.
Another common mistake is mixing types. If you create an ArrayList<String>, you can only add Strings to it. If you try to add a number or a different type, Java will stop you at compile time and tell you there's an error. This is actually a good thing — it prevents bugs.
A third mistake is assuming a List is empty when it's not, or assuming it has items when it doesn't. Always check the size before you try to get an item. You can write if (names.size() > 0) to make sure there's at least one item before you call get().
Frequently Asked Questions
What's the difference between ArrayList and a regular array?
An array has a fixed size set when you create it, and you can't change that size later. An ArrayList grows and shrinks automatically as you add and remove items. For most programs, ArrayList is easier to work with because you don't have to predict the exact number of items you'll need.
Can I create a List that holds different types of items?
Yes, but it's not recommended. You can write ArrayList<Object>, which holds any type of item, but then you have to check what type each item is before you use it. It's cleaner and safer to create a List for one specific type.
What happens if I remove an item while looping through a List?
This causes problems. The loop gets confused because the positions of items change as you remove them. If you need to remove items, collect them in a separate list first, then remove them after the loop finishes.
How do I sort a List?
Java provides a Collections.sort() method that sorts your List in place. For a list of Strings, write Collections.sort(names); and it will arrange them alphabetically. For numbers, it sorts from smallest to largest.
Can I copy a List?
Yes. You can create a new ArrayList and pass the original list to its constructor: ArrayList<String> copy = new ArrayList<String>(names); This creates a separate list with the same items. Changes to one list don't affect the other.