Loading...

Java List Methods

Keep an ArrayList<String> named tags, add oak then pine, and read the first value with tags.get(0). Compile Rack.java and run java Rack. The screen should show one line: oak. The call get(0) picks slot zero. Later demos on this page show size() printing 2 and remove(0) leaving [pine]. Reading one slot, counting, and dropping a slot are what this page teaches.

Read one slot with get

On the ArrayList page you printed the whole list and saw [oak, pine]. That full print still works. This page reaches one value at a time. The call tags.get(0) returns the word in slot zero. Slot numbers start at zero, the same idea as plants[0] on the arrays page, but the call uses parentheses: get(0), not brackets after the name alone.
Open Rack.java. Keep the public class name Rack so the file name matches. Delete leftover System.out.println(tags); as the only teaching line if you copied Shelf verbatim — this page’s first kept file prints tags.get(0) instead. Also delete any Scanner, nextLine, Bell.ring(), or package demo. The program must not wait for typing.
Near the top, above public class Rack, keep:
import java.util.ArrayList;
Inside main, create the list and add the same two words:
ArrayList<String> tags = new ArrayList<String>();
tags.add("oak");
tags.add("pine");
Then write:
System.out.println(tags.get(0));
Pass 0 inside the parentheses. Do not write tags[0]. Do not print the whole list as the required path for this first demo. Save before you compile.

Rack get first slot

java
import java.util.ArrayList;

public class Rack {
    public static void main(String[] args) {
        ArrayList<String> tags = new ArrayList<String>();
        tags.add("oak");
        tags.add("pine");
        System.out.println(tags.get(0));
    }
}

From the folder that holds Rack.java, run javac Rack.java. When parentheses, braces, angle brackets, and semicolons match, javac writes no line of its own. Then run java Rack — not java Rack.java, and not java ArrayList. Leave the .java ending off the run command.
The program should print one line and stop: oak. 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 create, the two add lines, and System.out.println(tags.get(0));.
Walk the run once. Java starts at Rack.main. The list grows with two adds. The call get(0) returns oak. The print line writes that word. The screen shows oak. Nothing else should print. If you see [oak, pine], you are still printing the whole list. Switch the print line to tags.get(0). If you see ding or 9, an older Porch or Pier program is running — use java Rack.
If get(1) is the print you keep by mistake, the screen shows pine. That is valid Java, but it is not the file this page keeps for the first demo. Keep get(0) so the screen is oak. If you call get(2) after only two adds, the program throws an error at run time because slot two was never filled. Stay with get(0) here.

Count with size

A second demo asks how many values sit in the list. After the same two adds, call tags.size() and print that number.

Rack size after two adds

java
import java.util.ArrayList;

public class Rack {
    public static void main(String[] args) {
        ArrayList<String> tags = new ArrayList<String>();
        tags.add("oak");
        tags.add("pine");
        System.out.println(tags.size());
    }
}

Compile and run the same way: javac Rack.java, then java Rack. The screen shows 2. The method size() returns a whole number. It does not print the words themselves. If the screen shows [oak, pine], you printed the list instead of size(). If the screen shows oak, you still have the get(0) print from the first demo. For this second file keep only System.out.println(tags.size()); after the two adds.
Do not invent tags.length on an ArrayList for this page. Fixed arrays use .length. Lists use .size(). Mixing those names is a common leftover from the arrays page. Keep size() for the list.

Drop a slot with remove

A third demo removes slot zero, then prints the list that remains.

Rack remove then print

java
import java.util.ArrayList;

public class Rack {
    public static void main(String[] args) {
        ArrayList<String> tags = new ArrayList<String>();
        tags.add("oak");
        tags.add("pine");
        tags.remove(0);
        System.out.println(tags);
    }
}

Compile and run again. After remove(0), oak is gone. Pine slides into slot zero. Printing the list shows [pine]. The screen is not [oak, pine] anymore. If you print tags.get(0) after the remove instead of the whole list, you would see pine alone — that shape is fine as a check, but the kept third file prints the list so you see the brackets with one word left.
Call remove with a slot number that exists. After two adds, remove(0) and remove(1) are both legal before either removal. This page keeps remove(0) so oak leaves first. Do not call remove(2) on a two-word list — that slot was never there.

Compared with ArrayList create and add

On the ArrayList page the teaching file stopped at println(tags) and the screen [oak, pine]. This page keeps the same create and add habit, then adds get, size, and remove. A leftover Shelf file that only prints the full list is history, not the quiz path here. For this page run java Rack with the demo you are studying: first get(0), then size(), then remove plus print.
You do not need a new import for these calls. They belong to ArrayList once the type is imported. Keep import java.util.ArrayList; at the top of each Rack file on this page.
When you later map keys to values, a following page — HashMap — starts that story. Stay with list calls here. Do not invent put or map syntax as a requirement on this page.

What this leaves for HashMap

This page finishes the basic list toolkit: read one slot, count values, drop a slot. The next page opens a different collection where each entry has a key and a value. Stay with get, size, and remove on ArrayList here. Do not invent HashMap lines in the Rack demos.
When old Shelf.java files still sit nearby, leave them alone or delete them only if they confuse which class you run. Run java Rack after you compile the file for the demo you mean.

Before HashMap

You can keep ArrayList<String> tags, add oak and pine, print tags.get(0) to see oak, print tags.size() to see 2, and after tags.remove(0) print the list to see [pine]. Compile Rack.java and run java Rack for each kept demo. The list methods did their job. The next page is HashMap, where a key points to a value in one object.
Keep the import. Keep the two adds before get, size, or remove. Keep slot zero as the teaching index on this page. Those habits are the checkpoint before you move on. When get(0) shows oak and size() shows 2, you are ready for the next topic.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
6
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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