A list in Java stores multiple items in a single container that you can add to, remove from, and access by position

A list is a data structure that holds a collection of items in order. Unlike an array, which has a fixed size, a list grows and shrinks as you add and remove items. Java's built-in list classes live in the java.util package, and the most common one is ArrayList. When you create a list, you specify what type of items it will hold — strings, numbers, objects — and then you can work with those items without worrying about managing the underlying storage yourself.

Lists are useful because they let you build collections of unknown size, iterate through items easily, and perform common operations like sorting or searching. You will use lists constantly in real programs: storing user names, collecting search results, building shopping carts, or holding game objects on a screen.

Key Takeaways

  • Create a list with ArrayList<String> names = new ArrayList<>();, replacing String with whatever type of item you want to store.
  • Add items with names.add("Alice"), remove them with names.remove(0), and access them by position with names.get(0).
  • Loop through a list with a for-each loop: for (String name : names) { System.out.println(name); }
  • Check the size with names.size() and clear everything with names.clear().
  • ArrayList is the most practical choice for most programs; LinkedList and Vector exist but are rarely needed.

Creating an ArrayList and Adding Items

To create a list, write the class name, the type in angle brackets, and call the constructor with new. Here is the basic pattern:

ArrayList<String> names = new ArrayList<>();

The angle brackets tell Java what type of object the list will hold. String means the list stores text. You could also write ArrayList<Integer> for whole numbers, ArrayList<Double> for decimals, or ArrayList<Person> if you have a custom class called Person. The empty angle brackets on the right side (<>) tell Java to figure out the type from the left side — this is called the diamond operator and it keeps your code cleaner.

Once you have created the list, add items with the add() method:

names.add("Alice"); names.add("Bob"); names.add("Charlie");

Each call to add() puts a new item at the end of the list. The list now has three items, and you can add more whenever you need to.

Accessing Items by Position

Every item in a list has a position, called an index, starting from zero. The first item is at index 0, the second at index 1, and so on. To get an item at a specific position, use the get() method:

String first = names.get(0); — this gives you "Alice" String second = names.get(1); — this gives you "Bob"

If you try to access an index that does not exist — say, names.get(10) when the list only has three items — Java will throw an IndexOutOfBoundsException and your program will crash. Always check the size first or use a loop that stops at the right place.

You can also replace an item at a specific position with set():

names.set(1, "Robert"); — this changes "Bob" to "Robert" at index 1

Removing Items and Checking Size

To remove an item, use remove() with the index of the item you want to delete:

names.remove(1); — this removes "Robert" (or "Bob" if you did not change it)

After removal, all items after that position shift down. If your list was ["Alice", "Bob", "Charlie"] and you remove index 1, it becomes ["Alice", "Charlie"]. Be careful when removing items inside a loop — the shifting can cause you to skip items or go out of bounds.

To find out how many items are in the list, use size():

int count = names.size();

This is useful for loops and for checking whether the list is empty. If you want to empty the entire list at once, call clear():

names.clear();

Looping Through a List

The most common way to go through every item in a list is a for-each loop:

for (String name : names) {   System.out.println(name); }

This reads as "for each name in names, print it". You do not need to worry about indices or size — Java handles that for you. This is the safest and clearest way to loop through a list when you just want to visit every item once.

If you need the index (the position) of each item, use a traditional for loop instead:

for (int i = 0; i < names.size(); i++) {   System.out.println(i + ": " + names.get(i)); }

This prints "0: Alice", "1: Bob", and so on. Use this pattern when you need to know the position or when you need to remove items as you loop (though even then, be careful about the shifting).

Other Useful List Methods

Java's ArrayList class includes many built-in methods beyond the basics. The contains() method checks whether an item is in the list:

if (names.contains("Alice")) {   System.out.println("Alice is in the list"); }

The indexOf() method finds the position of an item:

int position = names.indexOf("Bob");

If the item is not in the list, indexOf() returns -1. The isEmpty() method returns true if the list has no items:

if (names.isEmpty()) {   System.out.println("The list is empty"); }

You can also sort a list using the Collections.sort() method from the java.util package:

Collections.sort(names);

This sorts the list in alphabetical order (for strings) or numerical order (for numbers). After this call, your list is rearranged.

ArrayList vs. Other List Types

Java provides other list implementations, but ArrayList is the right choice for almost all programs. LinkedList is faster if you are constantly adding and removing items from the beginning or middle, but it is slower for accessing items by index. Vector is an older class that works like ArrayList but is synchronized (safe for multiple threads) — use it only if you are working with legacy code or building multi-threaded programs.

For learning and for most real-world work, stick with ArrayList. It is fast, straightforward, and has all the methods you need. The performance difference between ArrayList and LinkedList only matters in programs that process thousands of items repeatedly.

Frequently Asked Questions

Can I create a list that holds different types of items?

Yes, but it is not recommended. You can write ArrayList<Object> to hold any type of item, but then you have to cast items back to their original type when you retrieve them, which is error-prone. It is better to create separate lists for each type or to use a custom class that holds the different types you need together.

What happens if I add the same item twice?

The list will contain two separate entries of that item. If you add "Alice" twice, the list has two copies. If you call remove("Alice"), it removes only the first occurrence. Use contains() to check before adding if you want to prevent duplicates.

Can I create a list with a starting size?

Yes. ArrayList<String> names = new ArrayList<>(10); creates a list with space for 10 items, which can improve performance if you know roughly how many items you will add. The list still grows automatically if you add more than 10 items.

How do I convert a list to an array?

Use the toArray() method: String[] array = names.toArray(new String[0]); This creates a new array containing all the items from the list. The new String[0] tells Java what type of array to create.

Is it safe to remove items while looping through a list?

Not with a for-each loop — the list will shift items and you will skip or miss some. If you must remove items while looping, use an iterator or a traditional for loop that counts backwards, or collect the items to remove first and delete them after the loop ends.