Chapter 3 · Data

Arrays

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 21 · Concept

An array is a row of cells

An array keeps many values of one type under one name. int[] means "array of int", and String[] "array of String".

int[] temps = {12, 15, 9, 20, 17};

The array lives on the heap, and temps holds an arrow to it. Each value is an element, in a cell with an index from 0, as a String's characters have.

public class Temps {
    public static void main(String[] args) {
        int[] temps = {12, 15, 9, 20, 17};
        System.out.println("Days: " + temps.length);
        System.out.println("First: " + temps[0]);
        System.out.println("Last: " + temps[temps.length - 1]);

        temps[2] = 10;
        for (int i = 0; i < temps.length; i++) {
            System.out.print("Day " + (i + 1));
            System.out.println(": " + temps[i]);
        }

        int[] squares = new int[4];
        for (int i = 0; i < squares.length; i++) {
            squares[i] = i * i;
        }
        System.out.println("Last square: " + squares[3]);
    }
}
public class Warm {
    public static void main(String[] args) {
        int[] temps = {12, 15, 9, 20, 17, 14, 22};
        int warm = 0;
        // the first day is the highest so far
        int highest = temps[0];
        // loop over temps here
        System.out.println("Warm days: " + warm);
        System.out.println("Highest: " + highest);
    }
}
public class Names {
    public static void main(String[] args) {
        String[] names = {"Ada", "Grace", "Alan"};
        for (String name : names) {
            System.out.println("Hello, " + name);
        }
        for (int i = 0; i <= names.length; i++) {
            System.out.println(i + ": " + names[i]);
        }
    }
}
public class Names {
    public static void main(String[] args) {
        String[] names = {"Ada", "Grace", "Alan"};
        for (String name : names) {
            System.out.println("Hello, " + name);
        }
        for (int i = 0; i <= names.length; i++) {
            System.out.println(i + ": " + names[i]);
        }
    }
}
import java.util.Arrays;

public class Tools {
    static int[] doubled(int[] a) {
        int[] result = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = 2 * a[i];
        }
        return result;
    }

    static void addOne(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] += 1;
        }
    }

    public static void main(String[] args) {
        int[] numbers = {3, 1, 4, 1, 5};
        int[] twice = doubled(numbers);
        System.out.println(Arrays.toString(twice));
        System.out.println(Arrays.toString(numbers));
        addOne(numbers);
        System.out.println(Arrays.toString(numbers));
    }
}
import java.util.Arrays;

public class Reverse {
    static int[] reverse(int[] a) {
        // a new array: a's elements, last to first
        return a;
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int[] back = reverse(numbers);
        System.out.println(Arrays.toString(back));
        System.out.println(Arrays.toString(numbers));
        int[] pair = {7, 8};
        System.out.println(Arrays.toString(reverse(pair)));
    }
}
import java.util.Arrays;

public class Results {
    // the total of all the scores
    static int sum(int[] scores) {
        return 0;
    }

    // how many scores are limit or more
    static int countAtLeast(int[] scores, int limit) {
        return 0;
    }

    // a new array of the scores >= limit, in order
    static int[] atLeast(int[] scores, int limit) {
        return scores;
    }

    public static void main(String[] args) {
        int[] scores = {72, 95, 58, 81, 64, 90};
        System.out.println("Sum: " + sum(scores));
        System.out.print("At least 80: ");
        System.out.println(countAtLeast(scores, 80));
        int[] high = atLeast(scores, 80);
        System.out.println(Arrays.toString(high));
    }
}
public class Dice {
    public static void main(String[] args) {
        int[] rolls = {3, 6, 1, 3, 3, 6, 2};
        // counts[1] to counts[6]; counts[0] stays 0
        int[] counts = new int[7];
        // count each roll in counts
        for (int face = 1; face <= 6; face++) {
            System.out.println(face + ": " + counts[face]);
        }
    }
}
Terminal
Step 1 of 8An array and its indexes

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)