Heart containing Coding Chica Java 101

Elemental! Arrays in Java

TIP: References Quick List

What can we do if we have a set number of related items that we want to store together? In Java, an array might be the answer.

Array Indices

In Java we can store a collection of similar data as elements in an array. Arrays are created with a specified length – they have a fixed number of elements that can be stored. Elements in the array are referenced by the index (zero-based count) indicating where that item appears in the array.

Array Index0123
Array Value2531

In Java, we could create an array like the one described above in multiple ways:

Combined – Array Creation And Initialization

In the following code block, the creation of the array variable and initialization of the array elements are all done in a single statement.

    int[] bookCounts = { 2, 5, 3, 1 };

The square brackets [] indicate that an array is being requested. The [] can also be specified with the variable, but I prefer this format, as it reads well: An int array called shelfBookCounts.
Also, when we go to use the array variable, we will not include the [], so I like to have that consistent throughout my code, as well – separating type information from variable names.

However, if you see something like the following in Java, it is equivalent.

   int bookCounts[] = { 2, 5, 3, 1 };

In either version of the code above, we created and array with a length of 4 that contains the elements: 2, 5, 3, and 1. Each value to store as elements in the array are separated by a comma (,).

Separated – Array Creation, Then Initialization

When we populate the array’s elements separately from the creation of the array, we have to specify the length of the array. Then, when we want to populate the individual values in the array, we do so by specifying the index at which they should reside:

    int[] bookCounts = new int[4];
    bookCounts[0] = 2;
    bookCounts[1] = 5;
    bookCounts[2] = 3;
    bookCounts[3] = 1;

Here, the bookCounts[<index>] = <value>; format is used. Later, when we introduce loops, this can possibly be done in fewer lines of source code, but it is equivalent in the commands issued.

Accessing Array Elements

Once the array has been created and the values initialized, we can then reference those individual array elements with a similar syntax:

    System.out.println("Book Count 2: " + bookCounts[2]);

The command above would produce output like:

    Book Count 2: 3

Multi-Dimensional Arrays

Imagine you need to represent a checker board to track where the pieces are located. For this example, let’s do so with a player number (1 or 2) or 0 to indicate that the space is empty.

If you chose this approach, you would need to be careful to be consistent about whether width or depth is used first in your code. However, it could look something like:

    int[][] checkerBoard = {
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 2, 2, 2, 2, 2, 2, 2, 2 },
};

Just as before, each value to store in the array is separated by a comma (,). However, there is an outer array and individual inner array values to track.

Also, notice that there are two sets of square brackets, indicating that we want a 2-dimensional array of ints. The first array will point to another array object. The second array will contain int values. This representation breaks down once we add the idea of tracking checkers that have reached the other side of the board (kings), but this example was more for a visual representation of the concept. Java can support additional dimensions, too. However, just like with the two-dimensional array, as the number of dimensions increase, so does the complexity of trying to ensure that those dimensions are thought of and referenced in a consistent ordering. Often, I will find myself using custom objects, rather than multi-dimensional arrays to reduce such complexity.

Elemental! Arrays in Java

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.