Back to Top
 
 
 

Arrays

Until now we declared variables that stored a single value. But sometimes we have collections of data (e.g., days of the week, zodiac signs). Rather than create a variable for each of the elements we can create a single variable containing a set number of elements.

This is a characteristic feature of Java arrays: they have a predefined size that cannot be changed.

Another important thing is that an array can only contain elements of a single type.

We can imagine an array as a box with a set number of compartments.

Let's assume we have a box with seven compartments: one for each day of the week. The first compartment is Monday, second one: Tuesday, third: Wednesday and soon.

 

NOTE: Remember one, very important thing about arrays: they are numbered from zero! This "compartment number" in Java is called 'index'.

 

Here's an example:

String[] daysOfWeek = {"Monday",
 "Tuesday",
 "Wednesday",
 "Thursday",
 "Friday",
 "Saturday",
 "Sunday"};

Our Monday in the first compartment has an index of '0', Tuesday - in the second compartment - index '1'. That's why the number of the last index will always be smaller by one than the number of elements in the array.

In our case, the array has seven elements, so the last index (for the Sunday compartment) will be '6'. Thanks to this ordering of the values it is easy to reference each element of the array.

Array declaration

Let's try to declare an array. Declaring an array is similar to declaring a normal variable: we give its type and name. Another element is a set of square brackets that immediately inform us that we are dealing with an array.

Jeśli chcemy przypisać wartości, robimy to po znaku „=”, umieszczając w nawiasie klamrowym elementy rozdzielone przecinkiem.

If we want to assign values we do it by following the '=' character with elements divided by commas inside a square brackets.

 

Here's an example:

array_type[] array_name = { element1, element2, element3, element 4, and so on... };
 

Sample array declaration:

int[] arrayOfNumbers = {1, 2, 3};
 

Here's an example of referencing an element in the array:

array_name[index_number]; // we give the name of the array followed by the index number in square brackets.

See an example of declaring the table with a String type and displaying a specific element from it:

Displaying elements with a loop

We can conveniently display an entire array or its specific part using the 'for' loop we have learned earlier. Thanks to this we will not need to reference each element separately.

We are going to use the array with days of week we declared earlier and display all of its elements using a loop.

See the example of displaying the elements with a loop:

 

Tasks

 

Task 1

Declare two arrays (one with int type; the other - String) with any names and contents. Try to display several elements from each of them using their indexes.

Go to the first task:

Java arrays have attributes that allow finding some information about them. For example, we can check how many elements are in an array. This is revealed by the length attribute. We can check the length of our days of week array using daysOfWeek.length: that's the name of the array followed by a dot and the name of the attribute.

Task 2

Use the length attribute to check how many elements are in the arrays you declared in the previous task.

Go to the second task:

Task 3

Declare a twelve-element array with zodiac signs. Display the signs with the indexes of: 2, 5, 6, 7 and 11.

Go to the third task:

Task 4

Declare an array with planets of the solar system. Display the ones next to our planet referencing their indexes in the array.

Go to the fourth task:

Task 5

Declare an array with planets of the solar system. Display the ones next to our planet referencing their indexes in the array.

Go to the fifth task: