Java ArrayList
Create an ArrayList<String> named tags, call tags.add("oak"); then tags.add("pine");, and print the list with System.out.println(tags);. Compile Shelf.java and run java Shelf. The screen should show one line: [oak, pine]. The square brackets and the comma belong to how Java prints the list. You grow the list with add. That growing list after import is what this page teaches.
A list that can grow
On the arrays page a String[] held a fixed number of slots written in braces. You could not call add on that array. An ArrayList is a different kind of holder. You create it empty, then push values in with add. Each add grows the list by one. The print line shows every value currently stored, inside square brackets, separated by commas and spaces.
Open Shelf.java. Keep the public class name Shelf so the file name matches. Delete leftover import kit.Bell;, Bell.ring();, package kit;, and any Porch or Lobby call from the last pages. Those belong to the Classes module story. This page opens Collections. Also delete any Scanner, nextLine, or keyboard read. The program must not wait for typing.
Near the top of Shelf.java, above public class Shelf, write:
import java.util.ArrayList;
That import brings the ArrayList type into short-name reach the same way import kit.Bell; brought Bell on the import page. Without this line, javac cannot see the short name ArrayList on this page. Spell the package java.util and the class ArrayList carefully. Do not invent import java.util.*; as a requirement here — one exact import is enough.
Inside main, create the list:
ArrayList<String> tags = new ArrayList<String>();
The angle brackets hold String because this lesson stores words. The name tags is yours to type exactly as shown for the kept file. The new ArrayList<String>() call builds an empty list object. At this moment the list holds zero values. Printing it now would show [], but the kept file adds words before it prints.
Add oak, then pine, then print
Call tags.add("oak"); on its own line. Then call tags.add("pine"); on the next line. Each call stores one quoted word. Order matters for the screen: oak first, pine second. Do not swap them in the file you keep. Do not add a third word on this page’s teaching file — two adds are enough to see growth and the printed shape.
Then write:
System.out.println(tags);
Pass the list name itself. Do not wrap tags in quotes. Do not write System.out.println("oak"); by hand as the lesson path — the point is that the list object carries both words and prints them together. Save the file before you compile.
import java.util.ArrayList;
public class Shelf {
public static void main(String[] args) {
ArrayList<String> tags = new ArrayList<String>();
tags.add("oak");
tags.add("pine");
System.out.println(tags);
}
}
From the folder that holds Shelf.java, run javac Shelf.java. When parentheses, braces, angle brackets, and semicolons match, javac writes no line of its own. Then run java Shelf — not java Shelf.java, and not java ArrayList. Leave the .java ending off the run command. Start the class that holds main.
The program should print one line and stop: [oak, pine]. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call and keep the ArrayList create, the two add lines, and System.out.println(tags);.
Walk the run once. Java starts at Shelf.main. The import has already named which ArrayList you mean. The new line builds an empty list. The first add stores oak. The second add stores pine. The print line asks the list to show its contents. The screen shows [oak, pine]. Nothing else should print. If you see ding, 9, or ok, you are still running an older Porch, Lobby, Pier, or Yard program. Switch to java Shelf.
If javac says it cannot find ArrayList, the import line is missing, misspelled, or placed after public class Shelf. Move import java.util.ArrayList; above the class line. If you write tags.add(oak); without quotes, javac treats oak as a name that does not exist. Keep the quotes around each word. If you write String[] tags = new ArrayList<String>();, the kinds disagree and javac refuses — keep ArrayList<String> on both sides of the create line for this page.
Fixed array beside the growing list
A short contrast keeps the old arrays page in view without mixing it into the teaching run. A fixed String[] still works. It does not grow with add.
public class Shelf {
public static void main(String[] args) {
String[] trees = {"oak", "pine"};
System.out.println(trees[0]);
}
}
Compile and run that contrast only to remember the fixed shape. The screen shows oak — one slot — not [oak, pine]. There is no add call. To store a third word in a fixed array you would need a new array with three slots. The ArrayList teaching file is the one with import java.util.ArrayList;, two add calls, and System.out.println(tags);. Use the fixed-array file only as a reminder, not as the quiz path. Do not mix trees.add("elm") into that contrast — String[] has no add, and javac would refuse.
Side by side
One short table keeps the difference clear.
📊 ArrayList vs String array
| Piece | On this page |
|---|---|
| create | ArrayList<String> tags = new ArrayList<String>(); |
| grow | tags.add("oak"); then tags.add("pine"); |
| print list | System.out.println(tags); → [oak, pine] |
| fixed array | String[] trees = {"oak", "pine"}; — no add |
The table is only a reminder. The typed pieces you keep for the lesson file are the import, the ArrayList create line, the two add calls, and the print of tags. |
Compared with import and with arrays
On the import page, Porch called Bell.ring(); after import kit.Bell;. That still works as history. This page changes the cast to Shelf and the import to java.util.ArrayList. A leftover Bell.ring(); demo is a different lesson. For this page keep ArrayList<String> tags, two add lines, and print the list.
On the arrays page, braces filled a fixed String[]. That still works for a known size. This lesson chooses ArrayList so you see growth with add and a single print of the whole list. Do not mix plants[0] and tags.add("oak") in the same Shelf demo on this page. One clear path is enough: import, create, add, add, print.
When you later need get, size, or remove, a following page — list methods — covers those calls. Stay with create, add, and printing the list here. Do not invent a loop over the list as a requirement on this page; printing the list object is enough to see both words.
What this leaves for list methods
This page opens Collections with a list you can grow. The next page works the common list calls such as reading one slot and asking how many values sit inside. Stay with add and the full-list print here. Do not invent tags.get(0) as a requirement on this page unless you already know it; the lesson goal is the growing list and the [oak, pine] screen.
When old Porch.java or Lobby.java files still sit nearby, leave them alone or delete them only if they confuse which class you run. Run java Shelf after you compile the ArrayList file from this lesson.
Before list methods
You can import java.util.ArrayList, create ArrayList<String> tags, call tags.add("oak"); and tags.add("pine");, then print tags. Compile Shelf.java and run java Shelf. The screen shows [oak, pine]. The list grew with add. The next page is list methods, where you reach one value or ask the list how long it is.
Keep the import above the class. Keep both add calls before the print. Keep running java Shelf for this lesson. Those three habits are the checkpoint for this page before you move on. When the screen shows [oak, pine] after the two adds, the ArrayList did its job for this lesson and you are ready for the next topic.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top