1. java-hell.de
  2. Topics
  3. Arrays
Topic 5 of 11

Learn Java Arrays – Exercises Online

An array stores many values under one name. You'll create arrays, read elements by index, check the length and loop through them – including the classic ArrayIndexOutOfBoundsException.

The exercises

What you'll learn in this topic

  1. Your First Array
  2. Array Length
  3. Looping Through an Array
  4. Last Element
Exercise 1 of 4

Your First Array

An array stores multiple values of the same type:

int[] numbers = {10, 20, 30};

You access single values by their index — and it starts at 0:

numbers[0]  // 10
numbers[1]  // 20
numbers[2]  // 30

Change the index so that Wed gets printed.

Starter code
void main() {
    String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
    IO.println(days[0]);
}
Background
Arrays in memory

An array is one contiguous block of memory. int[5] reserves 5 × 4 bytes = 20 bytes in one piece. That's why index access is lightning fast — the JVM just does the math: start address + index × element size.

Arrays have a fixed size that's set when you create them and can never change. If you need a size that grows, use ArrayList.

Exercise 2 of 4

Array Length

With .length you get the number of elements:

int[] numbers = {10, 20, 30};
IO.println(numbers.length); // 3

Replace ??? to print the length of the array.

Starter code
void main() {
    String[] colors = {"Red", "Green", "Blue", "Yellow"};
    IO.println(???);
}
Background
.length on arrays vs. strings

On arrays, length is a field (no parentheses): arr.length. On strings, length() is a method (with parentheses): str.length(). Everybody mixes these up at first — don't worry.

The reason: arrays are a special construct built right into the language. They are objects, but they have no methods of their own for this — just a built-in, read-only length field. Strings are regular objects of the class String, and they give you their length through a method.

Exercise 3 of 4

Looping Through an Array

With for-each you walk through all elements — easier than using an index:

for (String s : myArray) {
    IO.println(s);
}

Read it as: "For each String s in myArray".

Complete the loop so that all fruits get printed.

Starter code
void main() {
    String[] fruits = {"Apple", "Pear", "Cherry"};

    for (String ???) {
        IO.println(fruit);
    }
}
Background
For-each under the hood

The for-each loop is syntactic sugar — the compiler turns it into a regular for loop behind the scenes. For arrays it uses an index, for collections an Iterator.

Downside: you don't get access to the index. If you need it, use the classic for(int i=0; ...) version.

Exercise 4 of 4

Last Element

The last element always has the index length - 1:

numbers[numbers.length - 1]

Replace ??? to print the last element — no matter how long the array is.

Starter code
void main() {
    String[] animals = {"Dog", "Cat", "Bird", "Fish"};
    IO.println(???);
}
Background
ArrayIndexOutOfBoundsException

If you access index 4 of an array with 4 elements, you get a runtime error — the program crashes. Java checks the bounds on every single access (bounds checking).

Languages like C don't do this check — there you just read whatever happens to be in memory (a classic source of security holes!). Java is safer here.

Enough reading. Your turn.

In the tutorial you write the code yourself, hit Run and instantly see whether your program works. No JDK, no IDE, no bullshit.

Practice Arrays in your browser