Arrays and ArrayList
1.5.1 — Arrays and ArrayList
An array stores several values in order. An ArrayList also stores values in order, but can grow or shrink.
String means text, written in double quotes. int means a whole number. System.out.println(...) prints a value. Each example contains its own data.
Read an array element
String[] means an array of Strings. Braces list its starting values.
An index is an element’s position, starting at 0. Use square brackets to read an element.
| Index | names[index] |
|---|---|
| 0 | Ada |
| 1 | Ben |
| 2 | Cleo |
names.length is the number of elements: 3. The last valid index is 2, not 3.
Replace one element
Use assignment to replace the value at an existing index. The array’s length stays the same.
The next cell changes only the second name, at index 1.
Visit every index
A for loop can visit all valid positions. Here, i starts at 0, continues while i < numberedNames.length, and increases by 1 after each print.
Use <, not <=: an index equal to the length is outside the array. Reading an invalid index causes an error when the code runs.
Visit each value directly
If you do not need the index, use an enhanced for loop:
for (String name : greetingNames)Read this as “for each String in greetingNames.” During each repetition, name holds the next value. + joins the greeting text and that name.
An empty array
{} creates an array with no elements. Its length is 0, and a loop over it runs zero times. Do not try to read index 0 from an empty array.
The next cell prints only its length and the message after the loop.
A list can change size
import java.util.ArrayList; makes the ArrayList class available. new ArrayList<String>() creates an empty list of Strings.
add("Ada")appends an element.get(0)reads the first element.remove(0)removes the first element. Later elements move down one index.size()gives the number of elements.
Like arrays, list indexes start at 0. Unlike an array, the list’s size changes when you add or remove elements.
Remove by index or by value
ArrayList has two remove methods with different arguments.
| Call | What it removes | Result returned |
|---|---|---|
remove(0) | The element at index 0 | The removed element |
remove("Ada") | The first element equal to Ada | true if one was removed; false otherwise |
With a String list, an integer argument selects an index, while a String argument supplies a value to find.
The next cell contains two Ada entries. Removing by value removes only the first match. Asking for a missing value leaves the list unchanged. Printing the list displays its values in square brackets.
This table summarises the [ArrayList API](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/ArrayList.html); you can use it without opening another page.
Practice: greet each name
The next cell prints each name. Change its print statement to add "Hello, " before the name.
Run it. The two lines should be Hello, Ada and Hello, Ben.
Hint
Change the print statement to System.out.println("Hello, " + name);.
Worked solution
String[] practiceNames = {"Ada", "Ben"};
for (String name : practiceNames) {
System.out.println("Hello, " + name);
}Try one name and no names
In your completed cell, replace only the array values after =:
| Array values | Expected output |
|---|---|
{"Ada"} | Hello, Ada |
{} | No output |
{"Ada", "Ben"} | Hello, Ada, then Hello, Ben |
Keep the loop unchanged. It works without accessing an element that might not exist.
Enable JavaScript to run the Java notebook.