JAVA · ОСНОВЫ · LESSON 13

Take a number.

Array index · about 14 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

First place is zero.

Java counts array locations from scratch. So the second number is at 1.

int[] nums = {10, 20, 30};
System.out.println(nums[1]);

✦ That's 20.

STEP 2

Don't go over the edge.

If there are three numbers in the array, addresses 0, 1 and 2 are available. Address 3 is overseas.

nums[0]
Addresses
[0]10[1]20[2]30

✦ Square brackets help to address one element.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

public class Main {
    public static void main(String[] args) {
        int[] nums = {10, 20, 30};
        System.out.println(nums[1]);
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create int[] fruits with numbers 4, 8, 12. Put the third number through its index.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework