You're recording daily temperatures for a week: 72, 75, 68, 71, 73, 76, 74. Rather than creating 7 separate variables (day1, day2, ...), you store them in one array and access each by its position.

Create an array with values

Declare an array and fill it with initial values.

Create.java
Replay: real traced execution (multi-file project)
public class Create {
    public static void main(String[] args) {
        int[] nums = {10, 20, 30, 40, 50};

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int first = nums[0];
        int second = nums[1];
        int last = nums[4];
    }
}
  1. first ← 10, second ← 20, last ← 50

    1public class Create {2    public static void main(String[] args) {3        int[] nums = {10, 20, 30, 40, 50};4        5        System.out.println("nums=" + java.util.Arrays.toString(nums));6        int first→ 10 = nums[0]10;7        int second→ 20 = nums[1]20;8        int last→ 50 = nums[4]50;9    }
    outputnums=[10, 20, 30, 40, 50]

The array holds 5 numbers. We can print each one by its position. Position starts at 0, not 1 - this is called zero-based indexing.

array A fixed-size container holding elements of the same type: `int[] nums = {10, 20, 30};`

Get elements by index

Access any element using its position number.

Get.java
Replay: real traced execution (multi-file project)
public class Get {
    public static void main(String[] args) {
        int[] scores = {85, 92, 78, 95, 88};

        System.out.println("scores=" + java.util.Arrays.toString(scores));
        int first = scores[0];
        int third = scores[2];
        int last = scores[4];
    }
}
  1. first ← 85, third ← 78, last ← 88

    1public class Get {2    public static void main(String[] args) {3        int[] scores = {85, 92, 78, 95, 88};4        5        System.out.println("scores=" + java.util.Arrays.toString(scores));6        int first→ 85 = scores[0]85;7        int third→ 78 = scores[2]78;8        int last→ 88 = scores[4]88;9    }
    outputscores=[85, 92, 78, 95, 88]
index Position in the array. First element is index 0, second is index 1, etc.
length `array.length` gives the number of elements in the array.

Put (change) elements

Modify values at specific positions.

Put.java
Replay: real traced execution (multi-file project)
public class Put {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};

        System.out.println("before: nums=" + java.util.Arrays.toString(nums));
        nums[0] = 100;
        nums[2] = 300;
        System.out.println("after: nums=" + java.util.Arrays.toString(nums));
    }
}
  1. nums[0] ← 100, nums[2] ← 300

    1public class Put {2    public static void main(String[] args) {3        int[] nums = {1, 2, 3, 4, 5};4        5        System.out.println("before: nums=" + java.util.Arrays.toString(nums));6        nums[0]→ 100 = 100;7        nums[2]→ 300 = 300;8        System.out.println("after: nums=" + java.util.Arrays.toString(nums));9    }
    outputbefore: nums=[1, 2, 3, 4, 5]
    after: nums=[100, 2, 300, 4, 5]

Arrays can be modified after creation. Assign a new value to any position.

Fibonacci with arrays

Store the entire Fibonacci sequence in one structure.

n
Fibonacci.java
Replay: real traced execution (multi-file project)
public class Fibonacci {
    public static void main(String[] args) {
        int n = 8;
        int[] fib = new int[n];

        fib[0] = 0;
        fib[1] = 1;
        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        System.out.println("fib=" + java.util.Arrays.toString(fib));
    }
}
public class Fibonacci {
    public static void main(String[] args) {
        int n = 5;
        int[] fib = new int[n];

        fib[0] = 0;
        fib[1] = 1;
        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        System.out.println("fib=" + java.util.Arrays.toString(fib));
    }
}
public class Fibonacci {
    public static void main(String[] args) {
        int n = 10;
        int[] fib = new int[n];

        fib[0] = 0;
        fib[1] = 1;
        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        System.out.println("fib=" + java.util.Arrays.toString(fib));
    }
}
  1. n ← 8, fib[1] ← 1

    1public class Fibonacci {2    public static void main(String[] args) {3        int n→ 8 = 8;  //@n=5, 104        int[] fib = new int[n];5        6        fib[0]0 = 0;7        fib[1]→ 1 = 1;8        for (int i = 2; i < n; i++) {
  2. fib[i] ← 1

    pass 1 of 6
    7fib[1] = 1;8for (int i2 = 2; i < n8; i++) {9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;10}
    All 6 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 1
    23110 2
    34210 3
    45320 5
    56530 8
    67850 13
  3. System.out.println("fib=" + java.util.Arrays.toString(fib));

    12    System.out.println("fib=" + java.util.Arrays.toString(fib));13}
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13]
  1. n ← 5, fib[1] ← 1

    1public class Fibonacci {2    public static void main(String[] args) {3        int n→ 5 = 5;4        int[] fib = new int[n];5        6        fib[0]0 = 0;7        fib[1]→ 1 = 1;8        for (int i = 2; i < n; i++) {
  2. fib[i] ← 1

    pass 1 of 3
    7fib[1] = 1;8for (int i2 = 2; i < n5; i++) {9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;10}
    All 3 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 1
    23110 2
    34210 3
  3. System.out.println("fib=" + java.util.Arrays.toString(fib));

    12    System.out.println("fib=" + java.util.Arrays.toString(fib));13}
    outputfib=[0, 1, 1, 2, 3]
  1. n ← 10, fib[1] ← 1

    1public class Fibonacci {2    public static void main(String[] args) {3        int n→ 10 = 10;4        int[] fib = new int[n];5        6        fib[0]0 = 0;7        fib[1]→ 1 = 1;8        for (int i = 2; i < n; i++) {
  2. fib[i] ← 1

    pass 1 of 8
    7fib[1] = 1;8for (int i2 = 2; i < n10; i++) {9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;10}
    All 8 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 1
    23110 2
    34210 3
    45320 5
    56530 8
    67850 13
    781380 21
    8921130 34
  3. System.out.println("fib=" + java.util.Arrays.toString(fib));

    12    System.out.println("fib=" + java.util.Arrays.toString(fib));13}
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Much cleaner than having c0, c1, c2... as separate variables.

Swap two elements

Exchange the first and last elements of an array.

Swap.java
Replay: real traced execution (multi-file project)
public class Swap {
    public static void main(String[] args) {
        int[] nums = {10, 20, 30, 40, 50};

        System.out.println("before: nums=" + java.util.Arrays.toString(nums));
        int temp = nums[0];
        nums[0] = nums[4];
        nums[4] = temp;
        System.out.println("after: nums=" + java.util.Arrays.toString(nums));
    }
}
  1. temp ← 10, nums[0] ← 50, nums[4] ← 10

    1public class Swap {2    public static void main(String[] args) {3        int[] nums = {10, 20, 30, 40, 50};4        5        System.out.println("before: nums=" + java.util.Arrays.toString(nums));6        int temp→ 10 = nums[0]10;7        nums[0]→ 50 = nums[4]50;8        nums[4]→ 10 = temp10;9        System.out.println("after: nums=" + java.util.Arrays.toString(nums));10    }
    outputbefore: nums=[10, 20, 30, 40, 50]
    after: nums=[50, 20, 30, 40, 10]

We need a temporary variable to hold one value during the swap. Without it, we'd lose one of the values.