Array (English array) is a powerful tool that allows you to work with a lot of data. Obviously, if you need to save, for example, 100 values โโsomewhere in the process of running your code, then making the same number of variables for this is at least unreasonable. An array allows you to store a large number of values โโunder one name and access them at the corresponding index. The concept of arrays is the cornerstone in learning a Java course for beginners. After all, they are the basis for many data structures.
Since Java is, first of all, OOP, compared with arrays in other programming languages, java array has one distinctive feature - they are represented as objects. Among other benefits, this eliminates the need to monitor memory cleansing, as it is freed automatically.
Creation and manipulation of one-dimensional arrays
A one-dimensional array is a classic Java array and is a collection of elements connected by a common name, each of which corresponds to a specific index. The way to declare an array is shown in the figure below.
First, a Java array type is declared, which defines the type of values โโstored in it. It can be any valid data type in Java. Next come the name of the array and square brackets that tell the compiler that the variable is an array. Pay attention to an important fact. Square brackets can be placed both after the base type of the array, and after the name of the array. After the equal sign, the new operator is indicated, initiating the allocation of memory for the array (as well as in the case of objects), the type of elements that will be stored in it (must be compatible with the base type declared earlier), and finally, their number indicated in square brackets.
The numbering of elements in the Java array begins with 0. So, the index of the first element in this array will be 0, and the sixth - 5. To refer to a specific element of the array, for example, the fifth, it is enough to specify the name of the array and the index of the element in square brackets next to the name . Thus, you can either assign a value to an element or retrieve it. However, you should be careful, because if you pass an index at which the element does not exist, an error will occur.
Multidimensional arrays in Java
Multidimensional arrays are rows of one-dimensional arrays referenced by elements of other arrays. In other words, these are arrays of arrays. The simplest among them are two-dimensional. Using their example, we will try to deal with the concept. For clarity, the figure below shows the syntax and scheme describing the structure of a two-dimensional array.
As you can see, the syntax is not very different from one-dimensional arrays. Let's analyze the structure. In the first brackets we allocated a space for 5 elements. These elements are nothing more than references to individual arrays. Moreover, the size of each of them is determined by the number in the second brackets. In fact, matrices are the equivalent of two-dimensional arrays in mathematics. Note that in addition to elements, a separate place is allocated in memory where the value of the length of the array (length) is stored. As a rule, work with multidimensional arrays is carried out through nested for loops.
Irregular Arrays
A two-dimensional array is an array of arrays. We have already found out. But can the arrays contained in it have different lengths? The answer is yes, they can. To do this, Java provides the ability to declare a two-dimensional array in a special way. For example, we want to create a two-dimensional array that would store three one-dimensional arrays of length 2, 3, and 4, respectively. It is declared as follows:
intarr [] [] = newint [3] [];
Please note that we did not indicate the number in the second brackets. Sizing arrays in arr is done like this:
arr [0] = new int [2];
arr [1] = new int [3];
arr [2] = newint [4];
Turning to the element at index 0 pointing to the first array, we declare it with dimension 2. An array with dimension 3 will be stored under the element with index 1, and so on. Everything is pretty simple.
Alternative java array declaration syntax
Arrays can also be initialized directly when they are created. It is pretty simple.
Note the declaration of jerseyNumber and playerName arrays.
In the case of two-dimensional arrays, this declaration looks like this:
int [] [] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
To do this, instead of the new operator, curly braces are opened, in which a list of all elements is separated by commas. Java in this case automatically allocates memory for them and indexes them accordingly.
Arrays helper class
To work with entities such as arrays in Java, the java.util package has a special Arrays class that provides many static methods that greatly facilitate the operation with them. The list of basic methods is presented in the figure below.
Let's look at some of the most useful Java array methods:
- copyOf (array, length) - returns a copy of the transmitted array of the appropriate length. If the transferred length is greater than the original array, then all the "extra" elements are filled with the default value (0 if the type is simple, and null if the reference).
- copyOfRange (array, first index, last index) - not specified in the figure, but a useful method. It copies the part of the passed array defined by the corresponding indexes, starting from the first and ending with the last.
- sort (array) - Sorts the elements of the array in ascending order.
- fill (array, value) - fills the passed array with the corresponding value.
- binarySearch (array, value) - returns the index under which the element with the corresponding value is in the transmitted sorted array. If such an element is absent, then a negative number is returned.
Since the methods are static, calling them does not require an instance of the Arrays class. They are called directly from it: Arrays.sort (arr).
Conclusion
We examined the most important aspects regarding arrays, and for those who are just starting to learn Java for beginners, this is enough for a basic understanding of such an entity as an array, and the basic techniques for working with it. Of course, practice will give more understanding of the operation of this tool. Therefore, do not be lazy to do several exercises, manipulating arrays in various ways.
The Array Java helper class is already used in "combat" conditions, so for starters it is recommended to learn how to do all the basic operations with arrays manually.