Task 1 of 9 · 0 solved · 9 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

5.1 Arrays — creating and indexing

Lesson

Storing many values together

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.


Worked example

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.


▶ 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]?


For int[] nums = {10, 20, 30};, match each access to its value.


Fill the index to read the third element of 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);
}}