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
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];
    }
}

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
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];
    }
}
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
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));
    }
}

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

Fibonacci with arrays

Store the entire Fibonacci sequence in one structure.

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

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

Swap two elements

Exchange the first and last elements of an array.

Swap.java
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));
    }
}

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