Massimo Caliman
by Massimo Caliman
2 min read

Categories

  • Java

Arrays are available in most programming languages because they realise the simplest organisation of complex data structures.

Declaration and allocation

Let us begin with some definitions that are a little more formal but which will be useful to fix the essential concepts.

Definition An array is a data structure that allows sequences of data of the same type to be represented. The elements that make up an array can be found by specifying their position.

Translated with www.DeepL.com/Translator (free version)

T[] var = new T[n];

defines an array of name var and type T of n elements, the elements of the array are

var[0], var[1], ... , var[n-1]

n also called the size or length of the array;

initialisation of elements at the same time as definition is also possible

T[] var = {values_0,value_1, ... ,value_n}

Hence, we have the values of the individual elements equal to

var[0] = value_0 ;var[1] = value_1 ;...var[n] = value_n ;

Otherwise, the declaration may take place in 2 stages

T[] var; var = new T[n] ;

or

var = new T[] { value_0,value_1, ..., value_n }

each element of the array can be used in the same way as a variable

given the array var { value_0,value_1, … , value_n }

var[0] = value_0 ; var[1] = value_1 ;

I can modify the second element of the array, i.e. var[1] with value new_value_1 with the instruction

var[1] = new_value_1 ;

transforming var into { value_0,new_value_1, … , value_n }

I can assign to a variable x of type compatible with T the i-th value of var simply like this

T x = var[i] ;

in this way x has the i-th value of the array var , obviously i must have a value between 0 and var.lenght0-1 representing the length of the array var.

Multidimensional arrays

The arrays seen so far are one-dimensional, i.e. they are linear sequences of data that are homogeneous and can be associated with a vector model. In reality, we can also have multidimensional arrays in which each element identified not by a single index but by a larger number of indexes.

for instance

T[][] var = new var [n][m];

the considerations made above for one-dimensional types apply, to initialise the position value or coordinate i,j I can write

var[i][j] = a_value ;

For smart initialisation, however, I can also do

T[][] var = { {value_0, value_1, ... , value_n} , { value_0, value_1, ... , value_m } }