An array is an object that contains one or more elements. Each element can contain a primitive data type or can contain references to objects.

All elements of an array must be of the same type, whether primitive or objects.

In Java, we have the length property that indicates the number of elements the array contains.

Syntax

The array can be declared across multiple statements, first creating the variable and allocating memory by specifying the length later:

tipo[] nombreDelArreglo;
nombreDelArreglo = new tipo[length];

For example:

double[] precios;
precios = new double(4);

or we can declare it directly on a single line:

tipo[] nombreDelArreglo = new tipo[length];
tipo nombreDelArreglo[] = new tipo[length];

For example:

double[] precios = new double(4);
double precios[] = new double(4);

To access an element in the array, we can use an index that indicates the position we want to access:

nombreArreglo[indice];

For example:

Precios[0] = 14.90;

Additionally, we have the option to assign data directly to the array:

tipo[] nombreArreglo = {valor1, valor2, valor3};

For example:

double[] precios = {13, 34.2, 56.89, 5}

To read an array without having to manually access each element by specifying its number, a for or while loop is required, so we will first need the size of the array using the length property:

nombreArreglo.length;

And to access it, we will use the loop index:

nombreArreglo[indice];

Two-dimensional arrays have two dimensions, which means we can interpret them as a table:

0123
01357
158910
256123524

To declare it, we will use the syntax:

tipo[][] nombreArreglo = new tipo[fila][columna]

If we now want to access the position where the data "3" is located, we will have to access the second column and the first row. Keep in mind that, in Java, the first column or row is 0, so we will need to use the following code:

matriz[0][1] = 3;

Arrays have many methods; you can find them all in the Oracle API.