# Arrays class and its use

Hello again! :)
In the last lesson, we got to know such a data structure called an array. We learned how to create them and fill them with data. And we looked at how they're stored in memory. Today we'll look at some tasks and examples of working with arrays that you will often see in real work. For example, imagine this situation: We have an array of 10 random numbers.
``````
int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};
``````
Our task is to sort this array in ascending order: from smallest to greatest. In the end, it should look like this: [-234, -2, 16, 26, 35, 43, 92, 99, 167] How do we do that? This task isn't trivial. We've never done this before :/ Any ideas? Try to guess. Here's one solution:
• Go through all the elements of the array. Compare each element with the next ( with ,  with ,  with , etc.). If the current element is greater than the next, we swap them, and then move on to the next element. If not, leave them as they are, and move on
• Thus, after the first pass through the elements, the largest value (167) is guaranteed to be in the last cell.
• Now we'll go through all the elements again, but this time we'll start with the index  to the penultimate element (the largest number is already in its place) and make the same comparisons and swaps. After this pass, in the penultimate cell, we'll have the second largest value (99).
• Repeat this process as many times as we have array elements.
We've got the idea. Now we only need to write the code. It looks like this:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};

for (int i = numbers.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
/* Compare the elements in pairs.
If they are not in the right order,
then swap them */
if (numbers[j] > numbers[j + 1]) {
int tmp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = tmp;
}
}
}

}
}
``````
Uh ... It looks a bit complicated -_- Even if the general principle is understandable, we still have to write quite a lot of code to solve such a simple task. Okay, maybe we've just overestimated ourselves? The task we've tackled is probably still too complicated for us. Let's try something simpler. For example, take the same number array.
``````
int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};
``````
Our task is to copy its contents to another array.
``````
int [] numbersCopy = new int;
``````
Think about how you would do it using what you already know about arrays? For example, you could go through `the numbers` array in a loop and sequentially write its elements into `numbersCopy`:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};

int [] numbersCopy = new int;

for (int i = 0; i < numbers.length; i++) {

numbersCopy[i] = numbers[i];
}

}
}
``````
Well, uh, here we've basically done it! It seems we've solved the problem. However, if you need to do this often, your code will have a bunch of identical loops. In fact, these (and other) tasks have long been solved by Java's creators. We don't need to "reinvent the wheel" and code our own solution. There is a special static class (`Arrays`) to help you perform common tasks when working with arrays. Methods for performing the most common tasks faced by Java programmers have been added to this class. For example, the task of sorting an array, which we tried to handle, is solved in a single line:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};

Arrays.sort(numbers);

System.out.println(Arrays.toString(numbers));

}
}
``````
The `Arrays.sort()` method sorts the array. And its algorithm is much more efficient than the code we wrote. Console output: [-234, -2, 16, 26, 35, 43, 80, 92, 99, 167] Note: To convert the array to a string, we used another method of the `Arrays` class: `Arrays.toString()`. Arrays in Java don't override the `toString()` method on their own. So, if you simply write
``````
System.out.println(numbers.toString());
``````
the Object class's `toString()` will be called. For an array, the output will be something like this: [I@4554617c We won't go into detail now about why exactly this is the output. The main thing is that it's clearly not what we need. But `Arrays.toString()` does precisely what we want. By the way, copying is also easily accomplished with the `Arrays` class:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};

int [] numbersCopy = Arrays.copyOf(numbers, numbers.length);
System.out.println(Arrays.toString(numbersCopy));

}
}
``````
We pass to the `Arrays.copyOf()` method our original array (from which we want to copy values) and the length of the new array into which we are copying data. In this case, we indicated `numbers.length` as the length, since want to copy the entire array. If we wanted to copy only the first few elements, we can specify the length of a new smaller array:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};

int [] numbersCopy = Arrays.copyOf(numbers, 4);
System.out.println(Arrays.toString(numbersCopy));

}
}
``````
Here we specified 4 as the length of the new array. Accordingly, only the first 4 elements of numbers will be copied to the new array. Console output: [167, -2, 16, 99] By the way, `Arrays` also lets you copy part of an array from the middle rather than the beginning of the array:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {167, -2, 16, 99, 26, 92, 43, -234, 35, 80};

int [] numbersCopy = Arrays.copyOfRange(numbers, 2,6);
System.out.println(Arrays.toString(numbersCopy));

}
}
``````
Output: [16, 99, 26, 92] Numbers were copied into the new array from the second array from the second (inclusive) to the sixth (not inclusive) element. We may also need to compare two arrays. As with the `toString()` method, the arrays themselves do not override the `equals()` method. So if we try to compare them like this
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {1, 2, 3};
int[] numbers2 = {1, 2, 3};

System.out.println(numbers.equals(numbers2));
}
}
``````
then we get false. This is because `Object.equals()`, which compares references, will be called. And, obviously, they are different! But what we need is to compare array contents, not references. The `Arrays` class overrides the `equals()` method to make it do exactly what we want:
``````
public class Main {

public static void main(String[] args) {

int[] numbers = {1, 2, 3};
int[] numbers2 = {1, 2, 3};

System.out.println(Arrays.equals(numbers, numbers2));
}
}
``````
Output: true By the way, the `Arrays` class works not only with ordinary arrays, but also with two-dimensional ones:
``````
public class Main {

public static void main(String[] args) {

int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int[][] numbersCopy = Arrays.copyOf(numbers, numbers.length);

System.out.println("Are these two-dimensional arrays equal?");
System.out.println(Arrays.deepEquals(numbers, numbersCopy));

System.out.println(Arrays.deepToString(numbersCopy));
}
}
``````
Output: Are these two-dimensional arrays equal? true [[1, 2, 3], [4, 5, 6], [7, 8, 9]] As you can see, the `Arrays.copyOf()` method was able to copy a two-dimensional array. And the class has special methods for comparing and displaying two-dimensional arrays: `deepEquals` and `deepToString()`. In the future, you'll repeatedly see (and rejoice in the fact that) that Java's creators anticipated a lot of situations frequently faced by programmers, and implemented ready-made solutions for them in the language. Using these solutions is much easier and more convenient than reinventing the wheel, right? :) Be sure to read the documentation for the Arrays class on the Oracle website. Good luck in your studies!
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
P.B.Kalyan Krishna Level 18, Guntur, India
18 December 2021
The equals() method in the Object class takes only one argument. For example if numbers and numbers2 are two one dimensional arrays then we check for their equality like this. if(numbers.equals(numbers2) which obviously returns false. But the override of the equals() method in the Arrays class is taking two arguments like Arrays.equals(numbers, numbers2). How is this possible? As far as I know the signature of both the methods must be same for overriding to be possible.
Justin Smith Level 30, Greenfield, USA, United States
11 July 2021
In the example where it talks about swapping two variable values, they create a 3rd variable "tmp". I've seen this done elsewhere. It is not necessary to create a 3rd variable (at least when working with numbers): int a = 5; int b = 2; a = a + b; b = a - b; a = a - b;
Albin Level 14, Chongqing, China
27 December 2020

Michael Level 10, Dresden, Germany
20 November 2020
Thanks, I enjoyed it very much!
Echapman Level 10, Webster Groves, United States
29 May 2020
I love Java!
Nickolas Johnson Level 8, St. Louis, United States of America
30 March 2020
There's a typo at: "Think about how you would you do it using what you already know about arrays"
Kev Carmichael Level 10, Caldas da Rainha, Portugal
3 March 2020
awesome
ezcard Level 0
16 February 2020
Awesome article.
P.B.Kalyan Krishna Level 18, Guntur, India
19 September 2019