Navigate Language Fundamentals topic: v  d  e )

An array is similar to a table of objects or primitive types, keyed by index. You may have noticed the strange parameter of the default main() method (String[] args) since the beginning of the book. It is an array. Let's handle this parameter:

Computer code Code listing 3.15: The default array parameter.
public class ArrayExample {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; ++i) {
      System.out.println("Argument #" + (i + 1) + ": " + args[i]);
    }
  }
}
Computer code Console for Code listing 3.15
$ java ArrayExample This is a test
Argument #1 This
Argument #2 is
Argument #3 a
Argument #4 test

In the code listing 3.15, the array is args. It is an array of String objects (here those objects are the words that have been typed by the user at the program launching). At line 4, One contained object is accessed using its index in the array. You can see that its value is printed on the standard output. Note that the strings have been put in the array with the right order.

Fundamentals edit

In Java, an array is an object. This object has a given type for the contained primitive types or objects (int, char, String, ...). An array can be declared in several ways:

  Code section 3.52: Array declarations.
int[] array1 = null;
int array2[] = null;

Those syntaxes are identical but the first one is recommended. It can also be instantiated in several ways:

  Code section 3.53: Array instantiations.
array1 = new int[10];
int[] array0 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //this only works in the declaration
array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

At line 1, we instantiate an array of 10 items that get the default value (which is 0 for int). At lines 2 and 3, we instantiate arrays of 10 given items. It will each be given an index according to its order. We can know the size of the array using the length attribute:

  Code section 3.54: The array size.
int nbItems = 10;
Object[] array3 = new Object[nbItems];
System.out.println(array3.length);
  Output for Code section 3.54
10

Arrays are allocated at runtime, so the specified size in an array creation expression may be a variable (rather than a constant expression as in C). However, the size of an instantiated array never changes. If you need to change the size, you have to create a new instance. Items can be accessed by their index. Beware! The first index is 0:

  Code section 3.55: The array indexes.
char[] array4 = {'a', 'b', 'c', 'd', 'e'};
System.out.println(array4[2]);
array4[4] = 'z';
System.out.println(array4[4]);
  Output for Code section 3.55
c
z

If you attempt to access to a too high index or negative index, you will get an ArrayIndexOutOfBoundsException.

Test your knowledge

Question 3.20: Consider the following code:

  Question 3.20: Question20.java
public class Question20 {
  public static void main(String[] args) {
    String[] listOfWord = {"beggars", "can't", "be", "choosers"};
    System.out.println(listOfWord[1]);
    System.out.println(listOfWord[listOfWord.length-1]);
  }
}

What will be printed in the standard output?

Answer
  Output for Question 3.20
can't
choosers

Indexes start at 0. So the index 1 point at the second string (can't). There are 4 items so the size of the array is 4. Hence the item pointed by the index 3 is the last one (choosers).

Two-Dimensional Arrays edit

Actually, there are no two-dimensional arrays in Java. However, an array can contain any class of object, including an array:

  Code section 3.56: Two-dimensional arrays.
String[][] twoDimArray = {{"a", "b", "c", "d", "e"},
                          {"f", "g", "h", "i", "j"},
                          {"k", "l", "m", "n", "o"}};

int[][] twoDimIntArray = {{ 0,  1,  2,  3,  4},
                          {10, 11, 12, 13, 14},
                          {20, 21, 22, 23, 24}};

It's not exactly equivalent to two-dimensional arrays because the size of the sub-arrays may vary. The sub-array reference can even be null. Consider:

  Code section 3.57: Weird two-dimensional array.
String[][] weirdTwoDimArray = {{"10", "11", "12"},
                               null,
                               {"20", "21", "22", "23", "24"}};

Note that the length of a two-dimensional array is the number of one-dimensional arrays it contains. In the above example, weirdTwoDimArray.length is 3, whereas weirdTwoDimArray[2].length is 5.

In the code section 3.58, we defined an array that has three elements, each element contains an array having 5 elements. We could create the array having the 5 elements first and use that one in the initialize block.

  Code section 3.58: Included array.
String[] oneDimArray = {"00", "01", "02", "03", "04"};
String[][] twoDimArray = {oneDimArray,
                          {"10", "11", "12", "13", "14"},
                          {"20", "21", "22", "23", "24"}};
Test your knowledge

Question 3.21: Consider the following code:

  Question 3.21: The alphabet.
String[][] alphabet = {{"a", "b", "c", "d", "e"},
                          {"f", "g", "h", "i", "j"},
                          {"k", "l", "m", "n", "o"},
                          {"p", "q", "r", "s", "t"},
                          {"u", "v", "w", "x", "y"},
                          {"z"}};

Print the whole alphabet in the standard output.

Answer
  Question 3.21: Answer21.java
public class Answer21 {
  public static void main(String[] args) {
    String[][] alphabet = {{"a", "b", "c", "d", "e"},
                          {"f", "g", "h", "i", "j"},
                          {"k", "l", "m", "n", "o"},
                          {"p", "q", "r", "s", "t"},
                          {"u", "v", "w", "x", "y"},
                          {"z"}};

    for (int i = 0; i < alphabet.length; i++) {
      for (int j = 0; j < alphabet[i].length; j++) {
        System.out.println(alphabet[i][j]);
      }
    }
  }
}

i will be the indexes of the main array and j will be the indexes of all the sub-arrays. We have to first iterate on the main array. We have to read the size of the array. Then we iterate on each sub-array. We have to read the size of each array as it may vary. Doing so, we iterate on all the sub-array items using the indexes. All the items will be read in the right order.

Multidimensional Array edit

Going further any number of dimensional array can be defined.

elementType[][]...[] arrayName

or

elementType arrayName[][]...[]