Java Tips — Managing arrays. An introduction for create, manipulate…
By: Marco Domenico Marino
Tags: Java, Programming Languages, Programming, Software Development

Java Tips — Managing arrays

An introduction for create, manipulate and use the arrays

Photo by Ilze Lucero on Unsplash

To store in memory more than a variable is possible to use the array; the arrays are a structure that may contain variables defined as primitive types or defined an object.

The syntax of an array declaration is characterized by the use of square brackets [].

Declaration and initialization of an array

To declare an array are required two information: name and type. Instead for the initialization is required the dimension. In details:

Name : the array is itself a variable and needs a name to be declared

: the array is itself a variable and needs a name to be declared Type : every array contains homogeneous variables and so is needed to define of the type of objects that intends to include into the array

: every array contains homogeneous variables and so is needed to define of the type of objects that intends to include into the array Dimension: the dimension of an array specify ahead the maximum number of variables that it can contain

Examples of array declaration

An array can be instantiated using the new command, specifying the number of object that can contain and that need to be positive, or specifying into the braces the list of values. In the first case the array will be initialized with the default values based on type, otherwise in the second case the array will have a dimension equal to several values contained in the list.

Examples of array initialization

Once an array object is created, its length never changes.

Access and scan an array

The positions in the array start with 0 until n-1 where n-1 is the maximum number of an object included in the array. To directly access a position into an array is needed specify the dimension-1 of the object that to get, for example:

Examples of direct access to an array

It is also possible to scan the entire or portion of an array using a for or while loop. During the loop it can be incremented or decremented the index that specifies the position of the variable into the array. In the next picture there are two example of scan (with for and while):

Examples of array scan (for and while loop)

In the next picture there is the console output of execution of the previous code:

Examples of array scan execution (for and while loop)

To avoid exceptions is important to maintain control to the index while scanning the array; the most common exception that can catch is ArrayIndexOutOfBoundsException that specify that the index used to access a position is less 0 or greater then dimension-1.

Array-of-array

The type of an array could be itself an array: in this way it is possible to create a multidimensional array that can be created, initialized and accessed like the array mono dimensional.

The declaration of an array-of-array expect that in the type definition is specified that the type is an array:

Examples of array-of-array declaration

To initialize the array now it is necessary to specify the dimension of the main array and the dimension of the array included into the main array; the dimension can be different and need to respect the rules of the mono dimensional array. The array included into the main array will have all the same dimension. Also in these case once an array object is created, its length never changes.

Examples of array-of-array initialization

To directly access a specified position of an array-of-array is needed to specify two coordinate: one coordinate get the position into the main array and the second coordinate get the position into the array got before.

Example of direct access to a specific position

The execution of this piece of code extracts the character “L” from the previous array declaration.

In the same way it is possible to scan entire or portion of an array-of-array by varying the two (or more) coordinate:

Examples of for and while loop used to scan the array-of-array

Conclusion

The array is the simplest data structure included in the Java SE: it is easy to declare, to initialize and to use.

In contexts where is possible to have the fixed dimension or is the direct access more frequent is recommended the use of array; otherwise in contexts dynamic where the dimension of the array is variable is recommended the Collection of the java.util.* library that implements many feature to simplify the use and the development.

Git: repository