ComputersProgramming

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 released automatically.

Creating and manipulating one-dimensional arrays

A one-dimensional array is a classical Java array and is a collection of common-named elements, each with a specific index. The method of declaring the array is shown in the figure below.

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 comes 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. Square brackets can be put both after the base type of the array, and after the name of the array. 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 (should be compatible with the base type declared earlier), and finally, their number , Indicated in square brackets.

The numbering of elements in the Java array starts at 0. So, the index of the first element in this array will be 0, and the sixth one will be 5. To refer to a specific element of the array, for example, to the fifth one, it is enough to specify the array name and the index of the element in square brackets next to the name . In this way, 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.

As you can see, the syntax is not very different from one-dimensional arrays. Let's analyze 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 analogues of two-dimensional arrays in mathematics. Note that in addition to the 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 by means of 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. 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 a 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];

Turning to the element under the index 0, pointing to the first array, we declare it with dimension 2. An element with 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.

Notice the declaration of the 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 brackets are opened, in which a list of all elements is separated by 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, the java.util package has a special Arrays class that provides a variety of static methods that greatly simplify operations with them. The list of basic methods is shown in the figure below.

Let's examine some of the most useful Java array methods:

- copyOf (array, length) - returns a copy of the transferred 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, 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 create an instance of the Arrays class to call them. They are called directly from it: Arrays.sort (arr).

Conclusion

We examined the most important aspects about arrays, and for those who are just starting to learn Java for beginners, this is enough for a basic understanding of an entity such as an array, and the 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 different ways.

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

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.