Java Array Basics

Create a Java array, fill it with values, and read each value by its index, which starts at 0.

Novice · Java · Updated

By

Definition

An array is a collection of elements, all of which are of the same type, stored in contiguous memory locations. Like our egg carton example, the length of an array is fixed once it is created. This means if you want to add more elements, you need to create a new array with a larger capacity.

Creating an Array

In Java, you can create an array using the following syntax:

// Creates an array to hold 5 integers
int[] testScores = new int[5];

Here, testScores is an array capable of holding 5 integer values.

Working with Arrays

Let's dive deeper into how to work with arrays by looking at some examples.

Example 1: Initializing and Accessing an Array

Consider the following example, where we create an array to store test scores.

int[] testScores = new int[5];
testScores[0] = 80;
testScores[1] = 85;
testScores[2] = 90;
testScores[3] = 75;
testScores[4] = 95;

// Accessing elements

// Outputs 80
System.out.println("First test score: " + testScores[0]);
// Outputs 95
System.out.println("Last test score: " + testScores[4]);

In this scenario:

  • We created an array named testScores with space for 5 integers.
  • We assigned values to each element using their respective indices.

It's important to note that array indices in Java start at 0. Therefore, the first element is accessed using index 0 and the last element using index 4 in an array with 5 elements.

Example 2: Using an Array of Strings

Let's consider another example where we work with an array of strings to store names.

String[] names = {"Alice", "Bob", "Charlie", "Diana", "Eve"};

// Accessing elements

// Outputs "Alice"
System.out.println("First student: " + names[0]);
// Outputs "Eve"
System.out.println("Last student: " + names[4]);

Here, we directly initialized the names array with String values.

Important Points to Remember

  • Fixed Size: Once you define an array's size, you cannot change it. If you need a larger array, you must create a new one and copy all elements of the old array into the new one.
  • Same Type: All elements in an array must be of the same type.
  • Indexed Access: You can access array elements using their index, starting from 0 up to array length minus 1.

Do it: see how much a student's score changed

A teacher wants to know how much each student's score changed from their first test to their last one.

The function gets one value:

  • testScores: one student's test scores, an int[], oldest first. It always holds at least one score.

For example:

int[] testScores = {72, 85, 90, 78, 88};

An array knows how many values it holds: testScores.length is 5 here, so the last index is testScores.length - 1, which is 4.

Index Score
The first test 0 72
The last test 4, which is testScores.length - 1 88
The change 88 − 72 = 16

So scoreChange(testScores) returns 16.

Your job: write the function int scoreChange(int[] testScores).

  • Return the last score minus the first score.
  • When the score went down, the answer is negative: {90, 80, 85} gives -5.
  • With only one score, the first and the last are the same test, so it returns 0.

The box holds the function with nothing around it. Since Java 25, a small program can be written this way, so the function needs no class and no public static in front of it.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets