What are arrays and their types

Java-Tutorial.org

Arrays

One-dimensional arrays

One-dimensional arrays (German: fields) are in principle simple lists. These arrays are declared with a data type, i.e. all values ​​that are to be stored in this array must be of the same data type with which the array was declared.

The array also has a fixed, unchangeable size that is stored in the length attribute and can be queried at runtime. If, for example, an eleventh element is accessed in an array and the array was only declared with a length of ten, a so-called java.lang.ArrayIndexOutOfBoundsException (Exception) thrown. Since exceptions will only be dealt with in a later article, it should only be mentioned briefly here that exceptions are thrown if an exception occurs during the runtime of a program (in this case, access to a non-existent element of the array).

The square brackets are characteristic of an array. These can be placed either between the data type and the array name or after the array name. Since an array is a complex data type, the new operator is required when creating the array.

Data type [] array name = new data type [length]; / * syntax for the declaration of an array * / data type array name [] = new data type [length]; / * syntax for the declaration of an array * /

In order to be able to address a certain element of the array, each element has a number, the so-called index. The first element gets the index 0, the second the index 1 etc. The following syntax is used to assign a value to an element of the array:

Array name [Index] = value; / * Syntax for the initialization of a field element * /

With the so-called initialization list, however, it is also possible to assign values ​​to the elements immediately in one step when creating the array. No new operator is then required here.

Data type [] Array name = {Element0, Element1, Element2, Element n-1, Element n}; / * Syntax for the literal initialization of an array * /

Let's look at an example of this:

Example:

int [] arr1 = newint [5]; / * Creation of an array with the field size 5 * / int [] arr2 = {1,2,3,4,5}; / * Initialization list of an array with the length 5 * /


In the example above, we used the new operator to create an array with the size 5 and the elements of which are of the data type int. However, the array does not yet contain any values. The second generation takes place via the initialization list. Each field element of an array is assigned a value directly.

To access an element, the index of the element must be specified after the array name in square brackets.

Example:

String [] names = {"Gustaf", "Hans", "Klaus", "Maria", "Gustaf"}; / * Initialization list of a string array * / String name = names [3]; / * Element at the position 3 is saved in the variable name (here: "Maria") * /

In the first line we create a string array with five names using the initialization list. In the second line we save the element with the index 3 in the variable name. In this case the variable contains the name "Maria" (don't forget: We start counting at 0!).

 

Multi-dimensional arrays

In Java, however, you can also create multidimensional arrays. These are nested arrays: the elements of the first dimension are arrays that themselves contain arrays of the second dimension, etc. Each dimension is represented by a pair of square brackets. For example, data from a table can be stored in a two-dimensional array. The first pair of brackets could then stand for the row, the second for the column.

Example:

double [] [] table = newdouble [20] [10]; / * table with elements of type double with 20 rows and 10 columns * /

When assigning a value or accessing an element, all indexes must be specified

Example:

table [11] [3] = 2.74;


An initialization list is also possible here again

Example:

int numbersArr [] [] = {{4,7,2}, {8,5,3,1}, {6,9}, {3,6,4}};

When creating objects in a multi-dimensional array, at least the size of the first dimension must be known.

The length attribute always indicates the size of the current dimension. If you want to address an element at the lowest level, the indices of all dimensions must be specified in full.

Example:

double line [] = table [5]; / * since the second index was not specified for the column, the entire sixth "table line" is read out as an array * / double value = table [5] [3]; / * is here the content of a single cell of the table is read out * /

 

Let's also look at three small exercises that will help you think about whether the examples are correct or not. For each example there is a button to find out the solution.

 Exercise 1:

int matrix [] [] [] [] = newint [1] [11] [12] []; / * four-dimensional array, where the first three dimensions are known * / int length = matrix [0] [0] .length ; / * The size of which dimension is stored in the variable length? * /




Exercise 2:

int matrix [] [] [] [] = newint [1] [] [12] []; / * four-dimensional array, where the first and third dimensions are known * /




Exercise 3:

int matrix [] [] [] [] = newint [] [] [] []; / * four-dimensional array, where no dimensions are known * /

 

The code from Exercise 1 is possible, but not very practical. The subsequent effort for complete initialization is immense. We are now using a small code example to show the initialization of a single field of the fourth dimension.

int matrix [] [] [] [] = newint [1] [11] [12] []; / * four-dimensional array, where the first three dimensions are known * / matrix [0] [0] [0] = newint [10]; / * Initialization of a field of the fourth dimension, where the index of the first three dimensions is 0 * /

If you want to completely initialize the fourth dimension afterwards, you would have to implement a triple nested for loop in this example. Therefore, it is best to know all dimensions directly from the first initialization. As a deterrent, let's show the effort involved in initialization with a triple nested for loop.

int matrix [] [] [] [] = newint [1] [11] [12] []; for (int i = 0; i As you can see, a new array has to be created for each field position. In our example, each field of the fourth dimension is assigned the size 10.