Skip to content

Java Arrays class Tutorial with Examples

Java Arrays class tutorial with examples will help you understand how to use the Java Arrays class in an easy way. The Arrays class in Java is a utility class provided by Java to manipulate arrays.

The Arrays class contains various utility methods to sort the array, search the array and fill the array with specified elements. The Arrays class also provides methods for copying an array to another array and converting an array to a List.

Java Collections and Arrays Utility classes

The Arrays class in Java is contained in the java.util package, so you have to import java.util.Arrays to use it in the code. The Arrays class is a part of the Java Collection Framework.

How to convert an array to String using the toString method?

The toString method returns a string representation of the specified array.

If the array elements are of primitive types, they are converted to the String using the valueOf method of the String class. If the array elements are objects, they are converted to String using the toString method. Each individual array elements are separated by “, ” (a comma followed by a space) and the whole string is enclosed in square brackets.

Output

How to sort an array using the sort method?

The static sort method allows an array to be sorted in ascending order, descending order or using a custom Comparator object. The default order of sorting is ascending order, however, you can also specify custom Comparator object to suit your needs. The below given sort method is overloaded to accept byte, short, char, int, long, float, double, and Object types.

For this example, we are going to sort an int array, but you can sort an array of any type mentioned above.

Output

How to sort a partial array using the sort method?

If you want to sort a partial array, you can use the overloaded version of the sort method having startIndex and endIndex parameters.

This method sorts the specified range of the array in ascending order. Here, the startIndex is inclusive while the endIndex is exclusive. This method throws ArrayIndexOutOfBoundsException if the startIndex is less than 0 or endIndex is greater than the array length.

Output

This method is also overloaded for byte, short, char, int, long, float, double, and Object types.

How to sort an array of custom objects using a Comparator using the sort method?

The below given overloaded sort method sorts an array of objects according to the order defined by the specified Comparator object.

The below given example sorts an array of custom objects in ascending and descending orders using the respective comparator objects.

Output

You can also use an overloaded sort method with the startIndex and endIndex parameters to sort the partial object array. The startIndex is inclusive, while the endIndex is exclusive.

Output

As you can see from the output, the Student object array is partially sorted. The array elements at index 0, 1, 2, and 3 are sorted while the element at index 4 (the last element) remains as is.

How to convert an array to List using the asList method?

The static asList method returns a fixed-size list that is backed by the original array.

Output

Please note that the list returned by asList method is backed by the original array. Any modification done to the original array is also reflected back to the list.

Output

How to search an element in an array using the binarySearch method?

The static binarySearch method allows us to search an element in an array using the binary search algorithm. The binarySearch method is overloaded for byte, short, char, int, long, float, double, and Object types.

Output

Important: The array must be sorted for the binarySearch method to work. It the array is not sorted, the result of this method call is undefined.

If the element is found, the binarySearch method returns an index of the element in the array. If the element is not found, the binarySearch method returns (-(insertion point) – 1) where the insertion point is an index of the first element greater than the key or the length of the array, if all array elements are less than the specified element.

Output

You can also search a partial array using the overloaded binarySearch method having start and end index parameters.

Here, the startIndex is inclusive while the endIndex is exclusive.

How to fill an array with the specified element using the fill method?

The static fill method allows us to fill an array with the specified element. The fill method fills all the elements of an array with the given value. The fill method is also overloaded to accept all primitive type arrays and objects.

Output

You can also fill the partial array with given elements by specifying the start and end index. To fill the partial array, use the overloaded fill method which accepts start index (inclusive) and end index (exclusive) as given below.

Output

How to compare two arrays using the equals method?

The equals method returns true if the specified two arrays are equal to one another. It returns false otherwise. The two arrays are considered equal if they both contain the same elements at the same index locations.

Output

Note: Two arrays are also considered equal if both of them are null.

The equals method is overloaded for byte, short, char, int, long, float, double, and Object types.

How to compare two multidimensional arrays using the deepEquals method?

The deepEquals method returns true if the specified two arrays are equal to one another. The equals method does not work for the multidimensional arrays, that is where the deepEquals method is useful. Consider below given example of comparing two multidimensional arrays using equals and deepEquals methods.

Output

What is the difference between equals and deepEquals methods? The equals method does not work with the multidimensional arrays, while the deepEquals method works for arrays with any number of dimensions.

How to copy an array to another array using the copyOf method?

The static copyOf method returns a new array having elements copied from the specified array.

If the specified length of the copied array is greater than the original array’s length, the remaining elements of the copied array are filled with the default value of the array type (which is 0 for int, null for an object). If the specified length of the copied array is less than the original array, the remaining elements of the original array are truncated and not included in the copied array.

Output

The copyOf method is overload for boolean, byte, short, char, int, long, float, double, and Object types. The copyOf method throws NegativeArrayIndexException if the specified length is less than zero, and NullPointerException if the original array is null.

How to copy a range of elements of one array to another using the copyOfRange method?

The copyOfRange method copies elements of the specified array from given start to end index into a new array. The copyOfRange method is overloaded to accept boolean, byte, short, char, int, long, float, double, and Object types. For this example, we will look at the int array.

This method copies the original array elements from start to end index to a new array. Here, the start index is inclusive while the end index is exclusive.

Output

Important: The copyOfRange method throws if the start index is less than 0 or greater than the array length. However, the end index can be greater than the array length. If this is the case, the copied arrays elements will be filled with the default value of the array type (which is 0 in case of int) as given in the above example. The copyOfRange also throws IllegalArgumentException if the start index is greater than the end index.

How to get Stream for an array using the stream method?

The stream method returns a sequential stream for the specified array as the source. The stream method is overloaded for int, long, double, and object types. For this example, we will have a look at an int array.

This method returns a sequential IntStream for the given array.

Output

Java Arrays Examples

References:
Arrays class Javadoc

Please let me know if you liked the Java Arrays class tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.