An array holds many values of the same type in order, under one name. You reach a value by its index — its position, counting from 0. So the first element is at index 0, the second at index 1, and so on.
int[] nums = {10, 20, 30}; // an array of three ints System.out.println(nums[0]); // 10 (first) System.out.println(nums[2]); // 30 (third)
nums[0] is the first element; nums[2] is the third. The number in the brackets is the index.
nums[0]
nums[2]
▶ Try it: run this, then change the values in the array or the index you print, and re-run.
An array stores...
For int[] nums = {10, 20, 30};, what is nums[0]?
int[] nums = {10, 20, 30};
For int[] nums = {10, 20, 30};, match each access to its value.
Fill the index to read the third element of nums.
nums
True or false?
Type exactly what this program prints.
public class Main { public static void main(String[] a){ int[] n = {5, 8, 2}; System.out.println(n[0]); System.out.println(n.length); }}