Arrays class in Java provides lots of useful utility methods for common array operations such as sorting, searching, comparing arrays, filling array elements, etc.
All these methods provided by the arrays class are static in nature. So, we do not need to create an instance of array class in order to use these utility methods.
We can call these utility methods by using the class name itself.
For example, a sort() method of arrays class can be used for sorting an array in ascending order. Similarly, binarySearch() method is used to search for a key in an array.
Arrays class is available in java.util.Arrays package and it is a member of Java Collections Framework. This class was introduced in Java 1.2 version.
Java Arrays Class declaration
Arrays class extends Object class. The general syntax to declare arrays class in java is as follows:
public class Arrays extends Object
The hierarchy diagram of this class is as below:
java.lang.Object java.util.Arrays
Before working with arrays, we need to import the statement java.util.Arrays class or entire java.util.* package in the java program.
Arrays Class Methods in Java
In addition to methods inherited from Object class, Java Arrays class also provides useful static methods for working with arrays.
Here, we have listed some most commonly important methods with example programs that are as follows:
1. List asList(): The asList() method returns a fixed-size list that is backed by the specified array. Let’s take an example program based on it.
Program code:
package arraysProgram; import java.util.Arrays; public class asListExample { public static void main(String[] args) { // Create an array. String[ ] cities = {"Dhanbad", "New York", "Delhi", "London", "Ranchi" }; // Call asList() method to convert the elements of array as List. System.out.println("String array as list: " + Arrays.asList(cities)); } }
Output: String array as list: [Dhanbad, New York, Delhi, London, Ranchi]
2. int binarySearch(): The binarySearch() method is used to search the index position of an element with a specific value.
In other words, this method is used to locate an element in an array by its value. It uses a binary search algorithm for searching. The binary search can be performed only on sorted arrays.
The return value is the index of the element that matches the specified value. It returns a negative value if the value is not present in the array.
The array and value must be the same type and can be any primitive data type and an object. The following general form of this method is as follows:
- static int binarySearch(byte array[ ], byte value)
- static int binarySearch(char array[ ], char value)
- static int binarySearch(double array[ ], double value)
- static int binarySearch(float array[ ], float value)
- static int binarySearch(int array[ ], int value)
- static int binarySearch(long array[ ], long value)
- static int binarySearch(short array[ ], short value)
- static int binarySearch(Object array[ ], Object value)
- static <T> int binarySearch(T[ ] array, int fromIndex, int toIndex, T value, Comparator<? super T> c)
- static <T> int binarySearch(T[ ] array, T value, Comparator<? super T> c)
Here, the last form throws an exception named ClassCastException if array contains elements that cannot be compared (for example, Double and StringBuffer) or if value is not compatible with the types in array.
In the last form, Comparator c is used to find out the order of the elements in the array.
Let’s create a program where we will find out the index position of elements in an array by their values.
Program code:
package arraysProgram; import java.util.Arrays; public class BinarySearchExample { public static void main(String[] args) { // Creating a sorted array of int and char types. int[ ] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}; System.out.println("1. Index of element 11 is " + Arrays.binarySearch(list, 11)); System.out.println("2. Index of element 12 is " + Arrays.binarySearch(list, 12)); char[ ] chars = {'a', 'c', 'g', 'x', 'y', 'z'}; System.out.println("3. Index of element 'a' is " + Arrays.binarySearch(chars, 'a')); System.out.println("4. Index of element 't' is " + Arrays.binarySearch(chars, 't')); // Creating unsorted array. int[ ] num = {25, 35, 10, 45, 20, 30}; Arrays.sort(num); // Sorting array. System.out.println("5. Index of element 45 is " +Arrays.binarySearch(num, 45)); } }
Output: 1. Index of element 11 is 4 2. Index of element 12 is -6 3. Index of element 'a' is 0 4. Index of element 't' is -4 5. Index of element 45 is 5
3. compare(array1, array2): This method is used to compare two arrays passed as parameters lexicographically. Both arrays array1 and array2 must have the same data types.
The compare() method returns 0 if the first and second arrays are equal and contain the same elements in the same order.
If the first array is lexicographically greater than second array, it returns a value greater than zero.
If the first array is lexicographically less than second array, it returns a value less than 0.
The compare() method has the following general form that are as:
- static int compare(boolean[ ] array1, boolean[ ] array2)
- static int compare(byte[ ] array1, byte[ ] array2)
- static int compare(char[ ] array1, char[ ] array2)
- static int compare(double[ ] array1, double[ ] array2)
- static int compare(float[ ] array1, float[ ] array2)
- static int compare(int[ ] array1, int[ ] array2)
- static int compare(long[ ] array1, long[ ] array2)
- static int compare(short[ ] array1, short[ ] array2)
- static <T extends Comparable<? super T>> int compare(T[ ] array1, T[ ] array2)
- static <T> int compare(T[ ] array1, T[ ] array2, Comparator<? super T> cmp)
The last two forms of compare() methods compare two Object arrays within comparable elements, lexicographically and by using a specified comparator respectively.
Let’s take an example program where we will compare elements of two arrays lexicographically.
Program code:
package arraysProgram; import java.util.Arrays; public class CompareArraysExample { public static void main(String[ ] args) { // Creating arrays of int type. int[ ] num1 = {1, 2, 3, 4, 5}; int[ ] num2 = {1, 2, 7, 4, 5}; int[ ] num3 = {1, 2, 3, 4, 5}; // Comparing both arrays lexicographically. System.out.println("\nComparing arrays using compare method:") System.out.println("Arrays.compare(num1, num2): " +Arrays.compare(num1, num2)); System.out.println("Arrays.compare(num2, num1): " +Arrays.compare(num2, num1)); System.out.println("Arrays.compare(num1, num3): " +Arrays.compare(num1, num3)); } }
Output: Comparing arrays using compare method: Arrays.compare(num1, num2): -1 Arrays.compare(num2, num1): 1 Arrays.compare(num1, num3): 0
4. compareUnsigned(array1, array2): This method is used to compare two arrays lexicographically, numerically treating elements as unsigned.
The compareUnsigned() method has the following general forms that are as:
- static int compareUnsigned(byte[ ] array1, byte[ ] array2)
- static int compareUnsigned(int[ ] array1, int[ ] array2)
- static int compareUnsigned(long[ ] array1, long[ ] array2)
- static int compareUnsigned(short[ ] array1, short[ ] array2)
Let’s take an example program based on it.
Program code:
import java.util.Arrays; public class CompareArraysExample { public static void main(String[] args) { // Creating arrays of int data type. int[ ] num1 = {1, 2, 3, 4, 5}; int[ ] num2 = {1, 2, 3}; // Comparing both arrays. System.out.println("\nComparing arrays using compareUnsigned method:"); System.out.println("Arrays.compareUnsigned(num1, num2): " +Arrays.compareUnsigned(num1, num2)); } }
Output: Arrays.compareUnsigned(num1, num2): 1
All the compare() method was added in JDK 9. JDK 9 also added compare() method to compare two arrays lexicographically over the specified ranges. They have the following forms:
- static int compare(boolean[ ] a, int aFromIndex, int aToIndex, boolean[ ] b, int bFromIndex, int bToIndex)
- static int compare(byte[ ] a, int aFromIndex, int aToIndex, byte[ ] b, int bFromIndex, int bToIndex)
- static int compare(char[ ] a, int aFromIndex, int aToIndex, char[ ] b, int bFromIndex, int bToIndex)
- static int compare(double[ ] a, int aFromIndex, int aToIndex, double[ ] b, int bFromIndex, int bToIndex)
- static int compare(float[ ] a, int aFromIndex, int aToIndex, float[ ] b, int bFromIndex, int bToIndex)
- static int compare(int[ ] a, int aFromIndex, int aToIndex, int[ ] b, int bFromIndex, int bToIndex)
- static int compare(long[ ] a, int aFromIndex, int aToIndex, long[ ] b, int bFromIndex, int bToIndex)
- static int compare(short[ ] a, int aFromIndex, int aToIndex, short[ ] b, int bFromIndex, int bToIndex)
Here, a and b represent different arrays.
5. copyOf(arrayOriginal, newLength): This method was added in arrays class in Java 6 version. It returns an array that is copy of arrayOriginal. newLength represents the length of copy.
If the length of copy is longer than arrayOriginal, then the copy is padded with zeros (for numeric arrays), nulls (for object arrays), or false (for boolean arrays).
If the length of copy is shorter than arrayOriginal, the copy is truncated. That is, the method does not copy all of the original array’s value.
If newLength is negative, a NegativeArraySizeException is thrown. If arrayOriginal is null, a NullPointerException is thrown. The copyOf() method comes in the following flavour.
- static boolean[ ] copyOf(boolean[ ] arrayOriginal, int newLength)
- static byte[ ] copyOf(byte[ ] arrayOriginal, int newLength)
- static char[ ] copyOf(char[ ] arrayOriginal, int newLength)
- static double[ ] copyOf(double[ ] arrayOriginal, int newLength)
- static float[ ] copyOf(float[ ] arrayOriginal, int newLength)
- static int[ ] copyOf(int[ ] arrayOriginal, int newLength)
- static long[ ] copyOf(long[ ] arrayOriginal, int newLength)
- static short[ ] copyOf(short[ ] arrayOriginal, int newLength)
- static <T> T[ ] copyOf(T[ ] arrayOriginal, int newLength)
Let’s take an example program where we will copy all elements of an array into another array.
Program code:
package arraysProgram; import java.util.Arrays; public class CopyArraysExample { public static void main(String[] args) { int[ ] arrayOriginal = {10, 20, 30, 05, 40}; int[ ] arrayNew = Arrays.copyOf(arrayOriginal, 5); printIntArray(arrayNew); } public static void printIntArray(int[] arrayNew) { System.out.println("New array:"); for(int arr : arrayNew) { System.out.print(arr); System.out.print(' '); } System.out.println(); } }
Output: New array: 10 20 30 5 40
If we write the statement int[ ] arrayNew = Arrays.copyOf(arrayOriginal, 2);, then the output will be 10, 20, 30.
If we write the statement int[ ] arrayNew = Arrays.copyOf(arrayOriginal, 7);, then the output will be 10, 20, 30, 5, 40, 0, 0.
6. copyOfRange(arrayOriginal, from, to): This method was also added in arrays class in Java 1.6 version. The copyOfRange() method returns a copy of range (from one index to another) within an array.
If the range is longer than arrayOriginal, the copy is padded with zeros (for numeric arrays), nulls (for object arrays), or false (for boolean arrays).
If from is negative or greater than the length of arrayOriginal, an ArrayIndexOutOfBoundsException is thrown. If from is greater than to, an IllegalArgumentException is thrown. If arrayOriginal is null, a NullPointerException is thrown.
The copyOfRange() method comes in the following flavour:
- static boolean[ ] copyOfRange(boolean[ ] arrayOriginal, int start, int end)
- static byte[ ] copyOfRange(byte[ ] arrayOriginal, int from, int to)
- static char[ ] copyOfRange(char[ ] arrayOriginal, int from, int to)
- static double[ ] copyOfRange(double[ ] arrayOriginal, int from, int to)
- static float[ ] copyOfRange(float[ ] arrayOriginal, int from, int to)
- static int[ ] copyOfRange(int[ ] arrayOriginal, int from, int to)
- static long[ ] copyOfRange(long[ ] arrayOriginal, int from, int to)
- static short[ ] copyOfRange(short[ ] arrayOriginal, int from, int to)
- static <T> T[ ] copyOfRange(T[ ] arrayOriginal, int from, int to)
Let’s create a program where we will copy a range of elements of an array into another array.
Program code:
package arraysProgram; import java.util.Arrays; public class CopyArraysExample { public static void main(String[] args) { char[ ] arrayOriginal = {'D', 'h', 'a', 'n', 'b', 'a', 'd'}; char[ ] arrayNew = Arrays.copyOfRange(arrayOriginal, 0, 7); // Copying with specified range. printIntArray(arrayNew); } public static void printIntArray(char[] arrayNew) { System.out.println("New array using copyOfRange method:"); for(char ch : arrayNew) { System.out.print(ch); System.out.print(' '); } System.out.println(); } }
Output: New array using copyOfRange method: D h a n b a d
7. deepEquals(): The deepEquals() method (added in arrays class in Java 5 version) returns true if the two specified arrays are deeply equal to one another. This method works for arrays of 2D or more dimensions. It has only one form:
- static boolean deepEquals(Object[ ] array1, Object[ ] array2)
It returns false if element values of arrays, or any nested arrays, differ. Let’s take an example program based on the deepEquals() method.
Program code:
package arraysProgram; import java.util.Arrays; public class deepEqualsExample { public static void main(String[] args) { int[ ][ ] array1 = {{20, 30, 40, 50}, {5, 10, 15}}; int[ ][ ] array2 = {{20, 30, 40, 50}, {5, 10, 15}}; System.out.println("Integer arrays on comparision: " +Arrays.deepEquals(array1, array2)); int[ ][ ] array3 = {{2, 3, 4, 5}, {5, 6}}; int[ ][ ] array4 = {{2, 3, 4, 6}, {5, 6}}; System.out.println("Integer arrays on comparision: " +Arrays.deepEquals(array3, array4)); } }
Output: Integer arrays on comparision: true Integer arrays on comparision: false
8. equals(array1, array2): The equals() method is used to check equality for one-dimensional array. It returns true if two arrays are equivalent. The equals( ) method comes in the following flavors:
- static boolean equals(boolean array1[ ], boolean array2[ ])
- static boolean equals(byte array1[ ], byte array2[ ])
- static boolean equals(char array1[ ], char array2[ ])
- static boolean equals(double array1[ ], double array2[ ])
- static boolean equals(float array1[ ], float array2[ ])
- static boolean equals(int array1[ ], int array2[ ])
- static boolean equals(long array1[ ], long array2[ ])
- static boolean equals(short array1[ ], short array2[ ])
- static boolean equals(Object array1[ ], Object array2[ ])
Let’s take an example program based on the equals() method where we will check equality for 1D array.
Program code:
package arraysProgram; import java.util.Arrays; public class EqualsExample { public static void main(String[] args) { int[ ] array1 = {20, 30, 40, 50}; int[ ] array2 = {20, 30, 40, 50}; System.out.println("Integer arrays on comparision: " +Arrays.equals(array1, array2)); int[ ] array3 = {2, 3, 4, 5}; int[ ] array4 = {2, 3, 4, 6}; System.out.println("Integer arrays on comparision: " +Arrays.equals(array3, array4)); } }
Output: Integer arrays on comparision: true Integer arrays on comparision: false
9. fill(array, value): The fill() method is used to fill an array with the specified value. It has the following general forms:
- static void fill(boolean array[ ], boolean value)
- static void fill(byte array[ ], byte value)
- static void fill(char array[ ], char value)
- static void fill(double array[ ], double value)
- static void fill(float array[ ], float value)
- static void fill(int array[ ], int value)
- static void fill(long array[ ], long value)
- static void fill(short array[ ], short value)
- static void fill(Object array[ ], Object value)
Let’s create a program to fill an array with a specified value.
Program code:
package arraysProgram; import java.util.Arrays; public class FillArrayExample { public static void main(String[] args) { int[ ] array = {20, 30, 40}; int value = 10; Arrays.fill(array, value); // Filling array with the specified value. System.out.println("Integer array after filling: " +Arrays.toString(array)); } }
Output: Integer array after filling: [10, 10, 10]
10. fill(array, from, to, value): This version of fill() method is used to assign a value to a subset of an array. It has the following general form:
- static void fill(boolean array[ ], int from, int to, boolean value)
- static void fill(byte array[ ], int from, int to, byte value)
- static void fill(char array[ ], int from, int to, char value)
- static void fill(double array[ ], int from, int to, double value)
- static void fill(float array[ ], int from, int to, float value)
- static void fill(int array[ ], int from, int to, int value)
- static void fill(long array[ ], int from, int to, long value)
- static void fill(short array[ ], int from, int to, short value)
- static void fill(Object array[ ], int from, int to, Object value)
All these methods may throw an IllegalArgumentException if from is greater than to, or an ArrayIndexOutOfBoundsException if from or to is out of bounds.
Let’s take an example program based on this method.
Program code:
package arraysProgram; import java.util.Arrays; public class FillArrayExample { public static void main(String[] args) { int[ ] array = {20, 30, 40, 50, 60}; int value = 10; Arrays.fill(array, 2, 4, value); // Filling array with a value within a range. System.out.println("Integer array after filling: " +Arrays.toString(array)); int[ ] array2 = {1, 2, 3, 5, 6, 7, 8}; int value2 = 4; Arrays.fill(array2, 2, 3, value2); System.out.println("Integer array after filling: " +Arrays.toString(array2)); } }
Output: Integer array after filling: [20, 30, 10, 10, 60] Integer array after filling: [1, 2, 4, 5, 6, 7, 8]
11. sort(array): The sort() method is used to sort the entire array in ascending order. It has the following general forms available:
- static void sort(byte array[ ])
- static void sort(char array[ ])
- static void sort(double array[ ])
- static void sort(float array[ ])
- static void sort(int array[ ])
- static void sort(long array[ ])
- static void sort(short array[ ])
- static void sort(Object array[ ])
- static <T> void sort(T array[ ], Comparator<? super T> c)
In the last form, Comparator c is used to order elements of array. The last two forms can throw an exception named ClassCastException if elements of the array being sorted are not comparable to each other.
12. sort(array, from, to): This version of sort() method is used to sort elements of an array within a specific range in ascending order. The following general forms of this method is available in java:
- static void sort(byte array[ ], int from, int to)
- static void sort(char array[ ], int from, int to)
- static void sort(double array[ ], int from, int to)
- static void sort(float array[ ], int from, int to)
- static void sort(int array[ ], int from, int to)
- static void sort(long array[ ], int from, int to)
- static void sort(short array[ ], int from, int to)
- static void sort(Object array[ ], int from, int to)
- static <T> void sort(T array[ ], int from, int to, Comparator<? super T> c)
Let’s take an example program where we will sort elements of an entire array and within a specific range in ascending order.
Program code:
package arraysProgram; import java.util.Arrays; public class SortingArrayExample { public static void main(String[] args) { int[ ] array = {20, 30, 80, 50, 60, 10}; Arrays.sort(array); // sorting entire array. System.out.println("Integer array after sorting: " +Arrays.toString(array)); int[ ] array2 = {8, 7, 1, 5, 4, 2, 0}; Arrays.sort(array2, 2, 7); // sorting array within a specific range. System.out.println("Integer array after sorting in a specific range: " +Arrays.toString(array2)); } }
Output: Integer array after sorting: [10, 20, 30, 50, 60, 80] Integer array after sorting in a specific range: [8, 7, 0, 1, 2, 4, 5]
13. toString(array): The toString() method is used to format elements of array in a string. Each element value is enclosed in brackets and are separated from each other with commas. It has the following general forms available in java:
- static String toString(boolean[ ] array)
- static String toString(byte[ ] array)
- static String toString(char[ ] array)
- static String toString(double[ ] array)
- static String toString(float[ ] array)
- static String toString(int[ ] array)
- static String toString(long[ ] array)
- static String toString(short[ ] array)
- static String toString(Object[ ] array)
14. stream(array): The stream() method was introduced in Arrays class in Java 8 version. It is used to convert an array into a stream of corresponding data types. It has the following overloaded general forms:
- static DoubleStream stream(double[ ] array)
- static IntStream stream(int[ ] array)
- static LongStream stream(long[ ] array)
- static <T> Stream<T> stream(T[ ] array)
15. stream(array, from, to): This version of stream() method is used to convert array into a stream with a specific range. It has the following overloaded forms available in java:
- static DoubleStream stream(double[ ] array, int fromInclusive, int toExclusive)
- static IntStream stream(int[ ] array, int fromInclusive, int toExclusive)
- static LongStream stream(long[ ] array, int fromInclusive, int toExclusive)
- static <T> Stream<T> stream(T[ ] array, int startInclusive, int endExclusive)
Let’s take an example program based on toString(), and stream() methods.
Program code:
package arraysProgram; import java.util.Arrays; import java.util.stream.IntStream; public class ToStringArrayExample { public static void main(String[] args) { String[ ] cities = {"Dhanbad", "Ranchi", "New York", "Delhi", "Bhopal"}; String nameOfCities = Arrays.toString(cities); System.out.println("Name of cities: " +nameOfCities); int[ ] num = new int[]{1, 2, 3, 4, 5}; IntStream nstream = Arrays.stream(num); System.out.println("Stream from a int array: " +nstream); } }
Output: Name of cities: [Dhanbad, Ranchi, New York, Delhi, Bhopal] Stream from a int array: java.util.stream.IntPipeline$Head@106d69c
16. parallelSort(array): This method is used to sort elements of an array of larger size in parallel. It can be used to sort array of primitive types or objects.
It was added in arrays class in java 8 version. It has the following forms available that are as follows:
- static void parallelSort(byte[ ] array)
- static void parallelSort(char[ ] array)
- static void parallelSort(short[ ] array)
- static void parallelSort(int[ ] array)
- static void parallelSort(long[ ] array)
- static void parallelSort(double[ ] array)
- static void parallelSort(float[ ] array)
- static <T> void parallelSort(T[ ] array, Comparator<? super T> cmp)
17. parallelSort(array, from, to): This form of parallelSort() method is used to sort an array in a specific range. It has the following overloaded version:
- static void parallelSort(byte[ ] array, int fromIndex, int toIndex)
- static void parallelSort(char[ ] array, int fromIndex, int toIndex)
- static void parallelSort(short[ ] array, int fromIndex, int toIndex)
- static void parallelSort(int[ ] array, int fromIndex, int toIndex)
- static void parallelSort(long[ ] array, int fromIndex, int toIndex)
- static void parallelSort(float[ ] array, int fromIndex, int toIndex)
- static void parallelSort(double[ ] array, int fromIndex, int toIndex)
- static <T extends Comparable<? super T>> void parallelSort(T[ ] array, int fromIndex, int toIndex)
- static <T> void parallelSort(T[ ] array, int fromIndex, int toIndex, Comparator<? super T> cmp)
Let’s create a program where we will sort elements of an array using parallelSort() method.
Program code:
package arraysProgram; import java.util.Arrays; public class ParallelSortExample { public static void main(String[] args) { String[ ] cities = {"Dhanbad", "Ranchi", "New York", "Delhi", "Bhopal"}; Arrays.parallelSort(cities); // Sorting whole array. String nameOfCities = Arrays.toString(cities); System.out.println("Using parallelSort(): " +nameOfCities); char[ ] chars = new char[ ]{'d','h','a', 'n','b','a','d'}; Arrays.parallelSort(chars, 0, 7); // Sorting the part of an array. System.out.println("Using parallelSort(): " +Arrays.toString(chars)); } }
Output: Using parallelSort(): [Bhopal, Delhi, Dhanbad, New York, Ranchi] Using parallelSort(): [a, a, b, d, d, h, n]
18. mismatch(array1, array2): The mismatch() method finds and returns the index of the first mismatch between two arrays. This methods returns -1 if no mismatch is found. It has the following overloaded version that are as follows:
- static int mismatch(boolean[ ] array1, boolean[ ] array2)
- static int mismatch(byte[ ] array1, byte[ ] array2)
- static int mismatch(char[ ] array1, char[ ] array2)
- static int mismatch(double[ ] array1, double[ ] array2)
- static int mismatch(float[ ] array1, float[ ] array2)
- static int mismatch(int[ ] array1, int[ ] array2)
- static int mismatch(long[ ] array1, long[ ] array2)
- static int mismatch(short[ ] array1, short[ ] array2)
- static int mismatch(Object[ ] array1, Object[ ] array2)
- static <T> int mismatch(T[ ] array1, T[ ] array2, Comparator<? super T> cmp)
19. mismatch(array1, from, to, array2, from, to): This form of mismatch() method finds and returns the relative index of the first mismatch between two arrays over the specified range.
Otherwise, returns -1 if no mismatch is found between arrays. It has the following versions available in Java:
- static int mismatch(boolean[ ] array1, int afromIndex, int aToIndex, boolean[ ] array2, int bFromIndex, int bToIndex)
Similarly, for char, byte, short, int, etc. We can also use the Comparator object for comparing.
Let’s take an example program based on the mismatch() method.
Program code:
package arraysProgram; import java.util.Arrays; public class MismatchArrayExample { public static void main(String[] args) { int[ ] num1 = {1, 2, 3, 4, 5}; int[ ] num2 = {1, 2, 7, 4, 5}; int[ ] num3 = {1, 2, 3, 4, 5}; // Finding mismatched index in arrays. System.out.println("Arrays.mismatch(num1, num2): " +Arrays.mismatch(num1, num2)); System.out.println("Arrays.mismatch(num1, num3): " +Arrays.mismatch(num1, num3)); System.out.println("Arrays.mismatch(num1, 0, 5, num2, 0, 1): " +Arrays.mismatch(num1, 0, 5, num2, 0, 1)); } }
Output: Arrays.mismatch(num1, num2): 2 Arrays.mismatch(num1, num3): -1 Arrays.mismatch(num1, 0, 5, num2, 0, 1): 1
20. hashCode(array): This method returns a hash code based on the elements of the specified array. It was introduced in arrays class in Java 1.5 version It has the following overloaded forms available in java:
- static int hashCode(boolean[ ] array)
- static int hashCode(byte[ ] array)
- static int hashCode(char[ ] array)
- static int hashCode(double[ ] array)
- static int hashCode(float[ ] array)
- static int hashCode(int[ ] array)
- static int hashCode(long[ ] array)
- static int hashCode(short[ ] array)
- static int hashCode(Object[ ] array)
Let’s take an example program where we will find hashCode of different arrays.
Program code:
package arraysProgram; import java.util.Arrays; public class deepEqualsExample { public static void main(String[] args) { int[ ] num1 = {1, 2, 3, 4, 5}; String[ ] names = {"Deepak", "John", "Herry", "Johnson"}; char[ ] chars = {'a', 'b', 'c', 'd', 'e'}; // Finding hashcode of arrays. System.out.println("Arrays.hashCode(num1): " +Arrays.hashCode(num1)); System.out.println("Arrays.hashCode(names): " +Arrays.hashCode(names)); System.out.println("Arrays.hashCode(chars): " +Arrays.hashCode(chars)); } }
Output: Arrays.hashCode(num1): 29615266 Arrays.hashCode(names): 346539009 Arrays.hashCode(chars): 121228546
Hope that this tutorial has covered almost all the important points related to arrays class in java and its various methods with different example programs. I hope that you will have understood this simple topic.
Thanks for reading!!!
Next ⇒ Array Copy in Java⇐ Prev Next ⇒