1. Introduction
Arrays is a utility class from the Java Collections API. The class contains various methods for array manipulation. In this tutorial, you will learn some of these methods.
2. Convert an array to a List
One of the most used scenarios is turning an array into a List. This is because the List interface provides many useful methods as compared to the simple array.
Integer[] anIntArray = {1,2,3,4,5};
List<Integer> listOfIntegers = Arrays.asList(anIntArray);
Note that for this to work, the items in the array must be of type object. If you are working with primitive types, you can use the associated wrapper classes.
The following code will not compile:
int[] anIntArray = {1,2,3,4,5};
List<Integer> listOfIntegers = Arrays.asList(anIntArray);//Does not compile
Beware of the following important points:
- Fixed-size: The List returned by this method is backed by the input array. This means that you can’t add any new element to the list. Any attempt to add or remove an element will throw an UnsupportedOperationException.
- Mutability: The list is backed by the input array. Any change in the list will reflect in the array, and vice-versa.
Integer[] anIntArray = {1,2,3,4,5};
List<Integer> listOfIntegers = Arrays.asList(anIntArray);
//listOfIntegers.add(6);//UnsupportedOperationException
listOfIntegers.replaceAll(i -> 2*i);
System.out.println(listOfIntegers);//[2, 4, 6, 8, 10]
System.out.println(anIntArray[4]);//10
anIntArray[4] = 20;
System.out.println(listOfIntegers.get(4));//20
3. Display an Array
As we saw in this tutorial, if you try to print an array to the console using System.out.println(myArray)
, Java will print a String corresponding to the memory address of your array. Hopefully, the Arrays
class has a toString()
method which provides a convenient way to display the content of an array.
int[] anIntArray = {1,2,3,4,5};
System.out.println(Arrays.toString(anIntArray));
4. Copy from Source to Destination
Another use case is copying the value of an array to another array. Like many other methods in the Arrays
class, the copyOf()
method is overloaded. This allows you to call it with any array type. The method accepts the original array as an input, and an integer denoting the size of the new array. If the integer is less than the original array size, the array is truncated. If on the contrary, it is greater than the original array size, the new array is padded with 0s.
int[] originalInArray = {1,2,3,4,5};
int[] copy1 = Arrays.copyOf(originalInArray,5);
System.out.println(Arrays.toString(copy1));// [1, 2, 3, 4, 5]
int[] copy2 = Arrays.copyOf(originalInArray,3);
System.out.println(Arrays.toString(copy2));// [1, 2, 3]
int[] copy3 = Arrays.copyOf(originalInArray,10);
System.out.println(Arrays.toString(copy3));// [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
5. Sort an Array
The Arrays class provides you with many methods to sort an array. The only condition is that the items in the array must either be primitive types or implement the Comparable
interface.
5.1. Sort an Array of Primitives
int[] anIntArray = {4,3,1,5,2};
Arrays.sort(anIntArray);
System.out.println(Arrays.toString(anIntArray));//[1, 2, 3, 4, 5]
5.1. Sort an Array of Objects
Below is an example with an object type Person
.
Person p1 = new Person("John", "Doe");
Person p2 = new Person("Alice", "Smith");
Person p3 = new Person("John", "Smith");
Person[] persons = {p1,p2,p3};
Arrays.sort(persons);
System.out.println(Arrays.toString(persons));
Output:
[Person[firstName=Alice, lastName=Smith], Person[firstName=John, lastName=Doe], Person[firstName=John, lastName=Smith]]
Note that for this to work, the Person class implements the Comparable
interface.
record Person(String firstName, String lastName) implements Comparable<Person>{
@Override
public int compareTo(Person other) {
// Compare by firstName first
int firstNameComparison = this.firstName.compareTo(other.firstName);
if (firstNameComparison != 0) {
return firstNameComparison;
}
// If firstName is the same, compare by lastName
return this.lastName.compareTo(other.lastName);
}
}
6. Miscellaneous methods
The Arrays utility class contains other useful methods among which: equals
, binarySearch
, compare
, etc.
int[] anIntArray1 = {1,2,3,4,5};
int[] anIntArray2 = {4,3,1,5,2};
int[] anIntArray3 = {1,2,3,4,5};
System.out.println(Arrays.equals(anIntArray1,anIntArray2));//false
System.out.println(Arrays.equals(anIntArray1,anIntArray3));//true
int pos = Arrays.binarySearch(anIntArray1,5);
System.out.println(pos);//4
Take away: The Arrays class provides very useful utility methods for array manipulation.
7. Conclusion
In this article, you learned some of the most used methods of the Arrays utility class.
You can find the complete code of this article here in GitHub.