Last Updated:

What is Java arrays? | Description, Examples, Methods

You can interpret Java arrays in much the same way. This is a tool that allows you to work with a lot of data. The key property that unites them is the homogeneity of the data.

Java arrays

Detailed description of the array

This name is given to the data structure. With the help of such an object, you can save components of a particular type. You can imagine such an element as a list of cells that are numbered. In each of them, you can put certain information data. It means that one element of information data will be placed in one cell.

Importantly! Knowing a particular number, you can access a single cell. This number is called an index in the software environment.

If we take Java specifically, the array is characterized by such a unique property as homogeneity. What does this mean in practice? This suggests that in all its cells there are elements that in one way or another belong to a single type.

For example, an array of numbers can only display integers. For this purpose, a variety of int is provided. An array of strings will only display the elements nested within them, that is, in this case, strings. If we create an array consisting of elements, for example, we denote them Dog, then the array will display these objects and no others.

This means that in Java, we can't put an integer in the first cell, some other non-integer value in the second, and elements of type Dog in the third. Everything should be homogeneous.

Methods of advertising

It is important to declare an array in java, just like any other variable. There are two key ways in which this can be put into practice. I must say that these options are equal, but the first of these is most consistent with the style of java. The second is a legacy taken from the C language.

Many programmers who worked with this language subsequently switched to Java. That is why a special, alternative method was created, which was left for their convenience. Look carefully at the table that shows you the above ways of declaring an array in Java.

 

Array shaping

Like any object, you can form an array, which means that you actually reserve a particular part of memory for it. In this, an operator called new comes to our aid. Here's what it all looks like in practice:

new typeOfArray [length];

The notation that immediately follows the operator indicates a type of array. The second term indicates the number of cells, in other words, the corresponding length. It can find expression in integers. True, here we only allocated a memory area for this object, but did not create an array bundle with any variable that was declared earlier. What do they usually do in this case? First, you declare an array, and then create it. Here's a concrete example:

int[] myArray; // declaring the array
myArray = new int[10]; // creation, in other words, we form a section of memory that belongs entirely to the 10 elements that are integers. They are designated as int.

In this example, we have an integer array of numbers. These integers have a specific name, namely myArray. After that, we gave a message that there are 10 cells in the structure. Each of them will somehow store one or another integer. However, much more often you can see the situation when an array is created immediately after the declaration.This is done by means of abbreviated syntax. Let's see what it looks like:

int[] myArray = new int[10]; announcement and allocation of memory "in one bottle"

Fact! As soon as we create an array using the above statement, we have a default value in all cells.

In our example, numerical varieties are given. For them, this value will be 0. To make it clearer, let's see what result we get after the operation:

int[] myArray = new int[10];

If we specify a value for numeric types, then for the boolean type it will correspond to the value false, while for reference varieties the null value will be relevant.

The result? An array of 10 integers is formed. Each of the cells will contain 0, but this will be relevant until we change the information laid down during the program.

Array length options

As mentioned earlier, the number of elements for which an object, called an array, is designed plays the role of the length of the array. Once this metric is created, you cannot change it. That is, this means that if there are 10 elements of the array, the first of the components of this object will correspond to zero, while the latter will have an index of 9.

There is a special tool with which you can access the length indicators. With respect to an array, the variable length is responsible for this. Let's take a closer look at how it will look in detail:

int[] myArray = new int[10]; // created an array of integers for 10 elements and named it myArray
System.out.println(myArray.length); // consoled the length of the array.

The final output of the program will be 10.

The process of initializing and gaining access to components

So, we figured out the nuances of creating an array in Java. Following the above rules, we can get an array in which the default values will be present. That is, it will no longer be an empty object. In the case of integers, these will be indicators equal to zero, but if the array has data of a reference type, then in each cell it will be possible to notice the information written in it in null format.

We now have access to the array element. What to do about it? We record in it any information data, values. You can also, alternatively, transfer it to the screen. It is also possible to carry out certain manipulations, operations on the index of the object.

Initialization should be considered as a process of filling in certain information. But this is not the data that we usually get by default.

Let's look at this process with an example. For example, let's create an array consisting of four seasons now. The values in it will be written in string format. Each of them will correspond to the names of the above seasons. Here's what we get in the end:

String[] seasons = new String[4]; /* declared and created an array. Java allocated memory for an array of 4 lines, and now in each cell there is a null value (since the string is a reference type)*/
seasons[0] = "Winter"; /* in the first cell, that is, in the cell with zero number, we wrote the string Winter. Here we access the zero element of the array and write there a specific value */
seasons[1] = "Spring"; // do the same procedure with cell number 1 (second)
seasons[2] = «Summer»; ... number 2
seasons[3] = "Autumn"; // and with the last, number 3

Now in each cell we see the names of the seasons and definitely will not get confused in the available information. But you can do a little differently the initialization described above. What are we going to do about it? Advertisement and initialization are compatible. What it will look like in the end:

String[] seasons = new String[] {«Winter», «Spring», «Summer», «Autumn»};

New, the operator we talked about earlier, can even be omitted. As a result, we get a record:

String[] seasons = {«Winter», «Spring», «Summer», «Autumn»};

Display on the monitor screen

On the console, in other words, on the screen, you can translate the elements of the array using specialized tools. The key one is the for cycle. There is another one that is shorter. This method involves the introduction of an array on the screen, but in this case we will try to consider the cyclic version. It looks like this:

String[] seasons = new String {«Winter», «Spring», «Summer», «Autumn»};
for (int i = 0; i < 4; i++) {
System.out.println(seasons[i]);

}

What we get on the program as a result of the data entered by us according to the algorithm:

Winter

Spring

Summer

Autumn

Manipulation of one-dimensional and multidimensional objects

Let's say we don't need to create an array of numbers or strings, or any objects. Imagine for a moment that you want to create an array of arrays. Is that possible? Java makes this possible.

There is a one-dimensional array, to which we are already accustomed. It is denoted as int[] myArray = new int[8]. The array of arrays that we're talking about now is called a two-dimensional array. In appearance, it resembles a table. It contains separate components, such as column number and row number. If you are familiar with the basics of linear algebra, then you will probably say that this object resembles a matrix:

 

Why and for what purpose is it necessary to create such arrays? With the help of such objects, it is allowed to program tables, matrices and any other objects that have a similar structure in their features. Imagine, for example, a field for playing chess. An 8 by 8 array could be used for it. Multidimensional variations of arrays can be declared and formed according to the following scenario:

Int[][] myTwoDimentionalArray = new int [8][8];

In total, such an array contains 64 elements: myTwoDimentionalArray[0][0], myTwoDimentionalArray[0][1], myTwoDimentionalArray[1][0], myTwoDimentionalArray[1][1] and so on up to myTwoDimentionalArray[7][7]

Therefore, if, for example, we imagine a chessboard, then the cell number A1 will be represented as myTwoDimentionalArray[0][0], and E2 as myTwoDimentionalArray[4][1].

Where 2 is possible, the presence of three is possible. That is, you can create 1 array of arrays, then form an array of arrays of arrays, and so on.

Fact! Despite the possibility of creating three-dimensional and more arrays, such objects are rarely used.

Be that as it may, a three-dimensional array will be required if you want, for example, to program a Rubik's Cube.