Java Array. Arrays in Java. Java for Beginners

An array is a powerful tool that allows you to work with a large amount of data. Obviously, if you need to store, for example, 100 values ​​in the process of working your code, then it is at least unreasonable to do the same number of variables for this purpose. The array allows you to store a large number of values ​​under the same name and access them at the appropriate index. The notion of arrays is the cornerstone of the Java course for beginners. After all, they are the basis for many data structures.

Since Java is primarily OOP, in comparison with arrays in other programming languages, java array has one distinctive feature - they are represented as objects. Among other advantages, this eliminates the need to monitor the memory cleanup, since it is automatically released.

Creating and manipulating one-dimensional arrays

A one-dimensional array is a classical Java array and is a collection of related items, each with a specific index. The way to declare an array is shown in the figure below.

java array

First, the Java array type is declared, which defines the type of values ​​stored in it. This can be any valid data type in Java. Next are the name of the array and the square brackets that tell the compiler that this variable is an array. Pay attention to an important fact. The square brackets can be set both after the base type of the array, and after the array name. After the equal sign, the operator new is introduced, which initiates allocating 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 equal to 0, and the sixth - 5. To access a specific element of an array, for example, the fifth one, 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 on which there is no element, an error will occur.

Multidimensional Arrays in Java

Multidimensional arrays are series of one-dimensional arrays that are referenced by elements of other arrays. In other words, these are arrays of arrays. The simplest among them are two-dimensional ones. On their example, we will try to understand the concept. For clarity, the figure below shows the syntax and schema describing the structure of a two-dimensional array.

arrays in java

As you can see, the syntax is not very different from one-dimensional arrays. Let's look at the structure. In the first brackets we have allocated a place for 5 elements. These elements are nothing more than references to individual arrays. The size of each of them is determined by the number in the second brackets. In fact, matrices are mathematically analogues of two-dimensional arrays. Note that in addition to the elements, a separate place is allocated in memory, where the length value of the array is stored. As a rule, work with multidimensional arrays is carried out by means of nested for loops.

java for beginners

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. For 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 specify the number in the second brackets. The definition of the size of arrays in arr is done like this:

arr [0] = new int [2];

arr [1] = new int [3];

arr [2] = newint [4];

Referring to the element under the index 0 pointing to the first array, we declare it with the dimension 2. An element with the index 1 will store an array of dimension 3, and so on. It's pretty simple.

The alternative syntax for the java array declaration

You can also initialize arrays directly when you create them. It's pretty simple.

class array java

Notice 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, braces are opened, in which all elements are enumerated through a comma. Java in this case automatically allocates memory for them and indexes them accordingly.

Auxiliary class Arrays

To work with entities such as arrays in Java, there is a special Arrays class in the java.util package, which provides many static methods that greatly simplify operations with them. The list of basic methods is shown in the figure below.

java array methods

Let's examine 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 simple type is, and null if the reference one).

- copyOfRange (array, first index, last index) - not specified in the figure, but a useful method. It copies the part of the transmitted array, determined by the corresponding indexes, from the first to the last.

- sort (array) - sorts the elements of the array in ascending order.

- fill (array, value) - fills the transmitted array with the appropriate value.

- binarySearch (array, value) - returns the index under which the element with the corresponding value is in the transmitted sorted array. If there is no such element, then a negative number is returned.

Because the methods are static, you do not need to instantiate an Arrays class to call them. They are called directly from it: Arrays.sort (arr).

Conclusion

We've looked at the most important aspects about arrays, and for those who are just starting to learn Java for beginners, that's enough for a basic understanding of an entity such as an array, and basic techniques for working with it. Of course, practice will give more understanding of the work of this tool. Therefore, do not be too lazy to do some exercises, manipulating arrays in various ways.

Auxiliary class Java Array is used already in "combat" conditions, therefore, for starters it is recommended to learn to perform all the basic operations with arrays manually.

Print Friendly, PDF & Email
Loading ...

Add a comment

Your e-mail will not be published. Required fields are marked *