Java arrays - a brief tutorial

An array is a container object that holds a finite number of values of a specific type. The number of elements the array can store is defined upon creation and cannot be changed afterward. After creation its length is fixed.

Array declaration

Declaring an array is very similar to declaring a variable. We need to specify a type and an identifier.

int[] numbers;

This line of code declares an array of integers with the name "numbers". Square brackets have to be used so that the compiler knows that numbers is an array-type. Names for arrays have to respect the usual Java variable naming conventions.

Creating and initializing an array

Now we need to create an array object and initialize our variable with it. Let's create an array with enough memory for 10 integers.

numbers = new int[10];


With the new operator, we create the array object and assign it to our numbers variable we created before. Instead of using two separate lines you can also use the shorthand syntax like this:

int[] numbers = new int[10];

If the array creation succeeded we can access each element in the array by using the square bracket syntax.

numbers[0] = 42; // initialize the first element
numbers[1] = 84; // initialize the second element
/// ...
numbers[9] = 21504 // initialize the last (10th) element

Since we start counting at index zero, our 10th element is at index 9. Using the number 10 inside the square brackets will cause a runtime error that may be caught via exception handling. In a real-world situation you would not access each element individually. Instead, you would use a loop structure like for or while.

Accessing array values

Of course, you not only want to store data inside an array but also read from it. In order to access individual array values you can use the square bracket operator as well.

//Prints the 6th element
System.out.println("Element at index 5: " + numbers[5]);


To print every item in the array we can use a loop that will iterate over each array.element.

for (int i = 0; i < numbers.length; i++)
{
System.out.println("Element at index " + i + ": " + numbers[i]);
}

The value numbers.length is the amount of elements that can be stored in the numbers array.

A quick note on array size

As previously mentioned, the array capacity is fixed and there is no way to change it. However, there is a neat little trick to circumvent this problem. Instead of resizing the original array (which is not possible), we create a new array with the new size and copy the old values into it. The Java array class offers a simple to use function for this approach.

If we want to increase the capacity of our numbers array from 10 to 20 we can use the following code:

numbers = Arrays.copyOf(numbers, 20);

This will create a new array with length 20 for numbers and copy the old values into it.
If you already know the array content at compilation time, you can also use the shorthand syntax like this:

int[] moreNumbers = {10, 20, 30, 40, 50, 60};

As you can see, there is no need to specify the array length when using the shorthand syntax.