Skip to lesson

Reading and changing array elements

Use name[index] to read an element or to put a new value in it. name.length tells you how many elements the array has.

Read and change one element

A re-check raises a mark from 32 to 35. An element works like a normal variable: marks[1] reads it, and marks[1] = … stores a new value there, leaving the other boxes alone.

Example 1 · Change a mark

GraceMarks.javaJava
public class GraceMarks {    public static void main(String[] args) {        int[] marks = {78, 32, 62, 90, 71};        System.out.println("Before: " + marks[1]);        marks[1] = marks[1] + 3;        System.out.println("After grace: " + marks[1]);        marks[4] = 75;        System.out.println("After re-check: " + marks[4]);    }}
Output
Before: 32
After grace: 35
After re-check: 75

What this code does

  • marks[1] = marks[1] + 3; reads 32, adds 3 and stores 35 back in the same box.
  • marks[4] = 75; replaces 71 with 75. The other three marks do not change.

The index can be any int expression, such as marks[i], marks[i + 1] or marks[n - 1]. Compound assignment (opens in a new tab) works on an element too: marks[1] += 5;.

Swapping two elements

To show the latest day first, you swap the first and the last temperature. Writing t[0] = t[3] straight away would wipe out the old t[0], so park it in a spare variable, temp, first.

Trace the code

Swap the first and the last element

public class SwapEnds {
public static void main(String[] args) {
int[] t = {31, 34, 29, 35};
int temp = t[0];
t[0] = t[3];
t[3] = temp;
System.out.println(t[0] + " " + t[3]);
}
}

Variables

t
{31, 34, 29, 35}

Output

(nothing printed yet)

Line 3The array starts with 31 in box 0 and 35 in box 3.

Step 1 of 5

length: how many elements

A register app does not know in advance how many days were recorded. present.length gives the number of elements, so the last index is always length - 1.

The array present has 6 boxes, 28 30 25 29 27 30, with indexes 0 to 5, so present.length is 6 and the last index is length - 1 = 5. Index 6 is outside the array: using it stops the program with ArrayIndexOutOfBoundsException.

Example 2 · length and the last element

ClassAttendance.javaJava
public class ClassAttendance {    public static void main(String[] args) {        int[] present = {28, 30, 25, 29, 27, 30};        System.out.println("Days recorded: " + present.length);        int last = present.length - 1;        System.out.println("Last index: " + last);        System.out.println("Last day: " + present[last]);    }}
Output
Days recorded: 6
Last index: 5
Last day: 30

What this code does

  • present holds how many students came each day, and present.length is 6.
  • present.length - 1 is 5, the last index. The same line works for an array of any size.

Write length without brackets. A String uses length() with brackets (String basics (opens in a new tab)), but present.length() on an array gives the compile error cannot find symbol.

Index outside the array

For 3 overs the valid indexes are 0 to 2, yet runs[3] still compiles. When that line runs, Java stops the program with an exception: an error that happens while the program runs.

Example 3 · Index outside the array

OutOfRange.javaJava
public class OutOfRange {    public static void main(String[] args) {        int[] runs = {4, 10, 7};        System.out.println("Over 1: " + runs[0]);        System.out.println("Over 4: " + runs[3]);        System.out.println("Done");    }}
Sample output
Over 1: 4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at OutOfRange.main(OutOfRange.java:5)

What this code does

  • The code compiles. Line 5 asks for runs[3], but the array has only the indexes 0 to 2, so the program stops there.
  • Done is never printed: once the exception happens, the lines after it do not run.

The message names the wrong index and the real length, and the at line gives the line number, so the mistake is quick to find. What are exceptions? (opens in a new tab) shows how to handle them; for now, keep every index between 0 and length - 1.

Find the bug

Find the wrong index

Select the line that is wrong.

This should print the last of 5 marks.

Round 1 of 4

Next step

Check your understanding

9 quick questions