Skip to content
Arrays and ArrayList
step 1/5

Reading — step 1 of 5

Learn

~3 min readCollections

Arrays and ArrayList

You'll rarely process one value at a time. Java gives you two workhorses for sequences: the array (fixed-size, minimal, fast) and ArrayList (growable, convenient). Knowing which one you're holding — and its slightly different API — matters daily.

Arrays — fixed size, direct access

int[] nums = {1, 2, 3, 4};        // literal
int[] zeros = new int[10];         // 10 slots, all 0

System.out.println(nums.length);   // 4 — a FIELD, no parentheses
System.out.println(nums[0]);       // 1 — indexes start at 0
nums[3] = 99;                      // write by index

The size is fixed at creation, forever. Valid indexes run 0 through length - 1; step outside and you get an ArrayIndexOutOfBoundsException at runtime:

int[] a = new int[4];
a[4] = 1;   // runtime crash — valid indexes are 0..3

The length / length() / size() trio

Three kinds of container, three spellings. Mixing them up is a compile error, and everyone does it for the first month:

array.length      // arrays: a field — no parens
string.length()   // Strings: a method
list.size()       // ArrayList and collections: a method, different name

ArrayList — when the size isn't known up front

import java.util.ArrayList;

ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.size());    // 2
System.out.println(list.get(0));    // 10

The <Integer> is a generic type parameter: this list holds Integers. Note it's Integer (the wrapper class), never int — Java generics only work with object types, so int values are auto-boxed on the way in.

Rule of thumb: known fixed size → array; grows as you go → ArrayList.

The scanning idiom — and the seed trap

The bread-and-butter pattern: walk a sequence tracking the "best so far." The trap is what you initialize the tracker to:

// BROKEN — seeds max with a value that may not be in the data:
int max = 0;
for (String p : parts) {
    int v = Integer.parseInt(p);
    if (v > max) max = v;
}
// input "-3 -1 -7" → prints 0. Zero isn't even in the list!
// CORRECT — seed with the FIRST element:
int max = Integer.parseInt(parts[0]);
for (int i = 1; i < parts.length; i++) {
    int v = Integer.parseInt(parts[i]);
    if (v > max) max = v;
}

Seeding with 0 silently assumes "all inputs are positive." The moment they aren't, 0 beats every real element and wins a contest it never entered. Seeding with the first element is always correct (Integer.MIN_VALUE also works).

Your exercise

Read one line of space-separated integers, split it (the starter's line.split(" ") gives you a String[] parts), convert each with Integer.parseInt, and print the largest.

The grader's hidden test is exactly the trap above: input -3 -1 -7 expects -1. If your submission prints 0 on that test, you seeded max with 0 — switch to parts[0]. Also note it's parts.length (array — no parens) when you loop by index, and a one-number line like 5 must print itself, which seeding from parts[0] handles for free.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Arrays and ArrayList — Java Fundamentals