JavaRush /Java Blog /Random EN /Java Arrays
articles
Level 15

Java Arrays

Published in the Random EN group

Arrays

An array is a data structure that stores values ​​of the same type. An individual array element is accessed using an integer index. For example, if a is an array of integers, then the value of the expression a[i] is equal to the i-th integer in the array. Java arrays - 1An array is declared as follows: first, the type of the array is indicated, that is, the type of elements contained in the array, followed by a pair of empty square brackets, and then the name of the variable. For example, here's how to declare an array consisting of integers:
int[] a;
However, this statement only declares a variable а, without initializing it with an actual array. To create an array, you need to use the operator new.
int[] a = new int [100];
This operator creates an array of 100 integers. The elements of this array are numbered from 0 to 99 (not from 1 to 100). Once created, the array can be filled, for example, using a loop.
int[] а = new int[100];
for (int i = 0; i < 100; i++)
 a[i] = i; //Заполняет массив числами от 0 до 99
If you try to access an element а [100](or any other element whose index is outside the range 0 to 99) by creating an array of 100 elements, the program will terminate because an array index out of range exception will occur . To count the number of elements in an array, use the Array name method .length. For example,
for (int i = 0; i < a.length; i++, System.out.println(a[i]));
Once an array is created, it is impossible to change its size (although you can, of course, change its individual elements). If you need to change the size of an array frequently during program execution, it is better to use another data structure called an array list. An array can be declared in two ways:
int[] a;
or
int a[];
Most Java programmers prefer the first style because it more clearly separates the array type int [](integer array) from the variable name.

Array initializers and unnamed arrays

Java has a facility for simultaneously creating an array and initializing it. Here is an example of such a syntactic structure:
int[] smallPrimes = {2, 3, 5, 7, 11, 13};
Note that in this case there is no need to use the operator new. Additionally, you can even initialize an unnamed array:
new int[] {16, 19, 23, 29, 31, 37}
This expression allocates memory for a new array and fills it with the numbers specified in curly braces. In this case, their number is calculated and, accordingly, the size of the array is determined. This syntactic construction is convenient to use to reinitialize an array without creating a new variable. For example, the expression
smallPrimes = new int{17, 19, 23, 29, 31, 37};
is a shortened expression
int[] anonymous = {17, 19, 23, 29, 31, 37};
smallPrimes = anonymous;
You can create an array of zero size. Such an array can be useful when writing a method that evaluates an array that turns out to be empty. A zero-length array is declared as follows:
new тип Элементов[]
Note that such an array is not equivalent to an object null.

Copying arrays

One array can be copied to another, but both variables will refer to the same array.
int[] luckyNumbers = smallPrimes;
luckyNumbers[5] = 12; //Теперь элемент smallPrimes[5]также equals 12
The result is shown in Fig. 3.1. If you need to copy all the elements of one array to another, you should use the arraycopy method from the class System. Its call looks like this:
System.arraycopy(from, fromlndex, to, tolndex, count);
The array tomust be large enough to contain all the elements to be copied. Java arrays - 2
Fig.3.1. Copying an array
For example, the operators shown below, the results of which are shown in Fig. 3.2, create two arrays and then copy the last four elements of the first array into the second. Copying starts from the second position in the source array, and the copied elements are placed in the target array starting from the third position.
int[] smallPrimes = {2, 3, 5, 7, 11, 13};
int[] luckyNumbers = {1001, 1002, 1003, 1004, 1005, 1006, 1007};
System.аrrаусору(smallPrimes, 2, luckyNumbers, 3, 4);
for (int i = 0; i < luckyNumbers.length; i++)
System.out.println(i + ": " + luckyNumbers[i]);
Executing these statements produces the following result.
0: 1001
1: 1002
2: 1003
3: 5
4: 7
5: 11
6: 13
Java arrays - 3
Rice. 3.2. Copying Array Elements
An array in Java is significantly different from an array in C++. However, it is practically the same as a pointer to a dynamic array. This means that the operator
int[] a = new int[100]; //Java
is equivalent to the operator
int* = new int[100]; //C++,
а не
int a[100]; //C++
In Java, the []default operator checks the range of indexes. In addition, Java does not have pointer arithmetic—you cannot increment a pointer to access the next element of an array. Link to source: Java arrays
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION