1. Arrays.fill()
When working with arrays, Java programmers very often need to fill the array with the same value. You can, of course, write a loop and simply assign some value to each cell of the array in the loop:
int[] x = new int[100];
for (int i = 0; i < x.length; i++)
x[i] = 999;
Or you can simply call the Arrays.fill()
method, which does exactly the same thing: it fills the passed array with the passed value. Here's how it looks:
Arrays.fill(name, value)
And the code in the example above can be made a little bit more compact and clearer:
int[] x = new int[100];
Arrays.fill(x, 999);
You can also use the Arrays.fill()
method to fill not the entire array, but a part of it, with some value:
Arrays.fill(name, first, last, value)
Where first and last are the indices of the first and last cells to be filled.
Example:
|
We're filling the cells x[3] , x[4] , x[5] , and x[6] with the value 999 . Cells of an array are numbered starting from zero!The str variable contains the value:
|
The Arrays.fill()
method only works with one-dimensional arrays. If you pass a two-dimensional array to the method, it will be treated as one-dimensional, with all the ensuing consequences.
2. Arrays.copyOf()
As you already know, you cannot resize an array after it has been created.
But what if you really want to?
Well, if you really want to, then you can!
- Create a new array of the desired length
- Copy all the elements from the first array into it.
By the way, this is exactly what the Arrays.copyOf()
method does. This is what calling it looks like:
Type[] name2 = Arrays.copyOf(name, length);
This method does not change the existing array, but instead creates a new array and copies the elements of the old array into it.
If the elements don't fit (the length is less than the length of the existing array), then the extra values are ignored.
If the length of the new array is greater than the length of the old one, the cells are filled with zeros.
Example:
|
The str2 variable contains the value:
The str3 variable contains the value:
|
3. Arrays.copyOfRange()
And what if you want to get an array of length 5 from an array of length 10, but you need it to contain the last 5 elements rather than the first 5?
In this case, another method of the Arrays
class will come in handy — the Arrays.copyOfRange()
. Here's what it looks like when we call it:
Type[] name2 = Arrays.copyOfRange(name, first, last);
This method also creates a new array, but fills it with elements from an arbitrary place in the original array. Where first and last are the indices of the first and last elements that should be put into the new array.
Example:
|
The str2 variable contains the value:
The str3 variable contains the value:
|
4. Arrays.sort()
Ah, the most delicious treat: sorting. Arrays are sorted quite often in programming. The three most common actions when working with arrays are:
- Sorting an array
- Finding the minimum (or maximum) element of an array
- Determining the index of an element in an array (finding an element in an array)
This is precisely why Java's creators included the sort()
method in the Arrays
class. This is what calling it looks like:
Arrays.sort(name);
This method sorts the passed array in ascending order.
Example:
|
The str variable contains the value:
|
Excellent, right? You called one method, and now you have a sorted array. Beautiful.
By the way, you can use this method to sort not only the entire array, but just part of it. This is what calling it looks like:
Arrays.sort(name, first, last);
Where first and last are the indices of the first and last cells that the sort should touch.
Example:
|
The str variable contains the value:
|
To sort arrays, Java uses the fastest sorting algorithm — QuickSort. Its computational complexity depends on the size of the array and is calculated using the formula N log(N).
Sorting an array of 1000 elements will involve about 3,000 comparisons of array elements. Sorting an array of one million elements will involve about 6 million comparisons.
5. Arrays.binarySearch()
Well, and the last of the most interesting methods of the Arrays
class is able to search for a given value in an array. This is no ordinary search — it is the beloved binary search. It boils down to this:
- First, the array is sorted.
- Then the middle element of the array is compared with the one we are looking for.
- If the element is greater than the middle element, then the search continues in the right half of the array.
- If the element we are looking for is less than the middle element, then the search continues in the left half of the array.
Because the array is sorted, it is possible to eliminate half of it in a single comparison. Then in the next step, we toss out another half, and so on.
This approach makes binary search very fast. In an array of one million (!) elements, it can find the index of the desired element in just 20 comparisons. The approach's shortcoming is that the array must first be sorted, and sorting also takes time.
This is what calling it looks like:
int index = Arrays.binarySearch(name, value);
Where name
is the name of the array, which must be passed already sorted (for example, using the Arrays.sort()
method). And value
is the element we are searching for in the array. The result returned by the method is the index of the desired array element.
Examples:
|
x is:
4 1 (indices 0 and 2 are also acceptable)8 -7 |
If the array contains multiple instances of the element being searched for, then the algorithm will simply return the index of one of them (there is no guarantee that it will be, say, the very first, or vice versa — the very last of these duplicate elements)."
If the element is not found in the array, then the index will be negative.
6. Link to Oracle documentation on the Arrays
class
If you are super interested, you can read everything about the Arrays
class and all its methods in the official documentation on the Oracle website.
For example, you can read about the Arrays.mismatch()
and Arrays.compare()
methods. Maybe you will find them useful somehow.
And don't be confused by the number of methods. Each method has 5-10 variants, which differ only in their parameter types.
GO TO FULL VERSION