We have seen how Collections class methods made Java Programmer job easy to operate on data structure elements and we have discussed nearly 25 operations like comparing, sorting and searching etc. with good example codes. Now it is the turn with Arrays class. Almost the same methods exist with Arrays class but useful to perform on array elements. Array class was introduced with JDK 1.2 and placed in java.util package and forms a part of collections framework. Before JDK 1.2, all the array elements manipulations (like shuffling, sorting and searching) were done by the developers from scratch level with enormous looping and if codes.
The general exception thrown by all the methods is NullPointerException if the array reference is null.
Following is the class signature
Following are the important methods of Arrays class and all are static methods so that they can be called with Arrays class directly (without the need of creating object).
class Arrays API Methods
- static void sort(int array1): Sorts all the elements of the array array1 in ascending numerical order. The sorting algorithm used is Quicksort (Mergesort) with Collections.sort()). The sort(int array1) method is overloaded taking arrays of different data types like long array, short array, char array, byte array, double array, float array and Object array as parameter. That is, the sort() method is capable to sort an array with any data type or objects.
- static void sort(int aray1, int startIndex, int endIndex): This is a modified form of the previous sort() method useful to sort a few elements of the array (not all elements). The elements falling between startIndex to endIndex-1 are sorted. This method is overloaded to accept any type of array. Two exceptions thrown by this method are IllegalArgumentException when startIndex is greater than endIndex and ArrayIndexOutOfBoundsException when the index numbers are out of range.
- static void sort(Object array1, Comparator comp1): Sorts all the array1 elements in the order as per the Comparator comp1. Here, the elements must be compatible to get sorted (should be of same data type) else ClassCastException is raised.
- static void sort(Object array1, int startIndex, int endIndex, Comparator comp1): It is a modified form of the previous method useful to sort a few elements of the array. If the comparator is given as null, the elements are sorted in natural order, by default.
- static int binarySearch(int array1, int searchNum): Searches the element searchNum in the array array1. This method should be used in combination with sort() method. Before applying this method, the elements of the array should be sorted with sort() method; else unexpected results are expected. This method is overloaded to accept an array of any data type. It is just similar to Collections.binarySearch().
- static int binarySearch(Object array1, Object searchNum, Comparator comp1): It is a modified form of the previous search where the searching is done as per the order set by Comparator comp1. Before applying this method, the elements should be sorted in an order as per the comparator comp1; else unexpected results are expected. It is just similar to Collections.binarySearch() with comparator.
- static boolean equals(int array1, int array2): Useful to compare the elements of two arrays. Returns true if both arrays array1 and array2 contain the same elements; here order is also important. This method is overloaded to accept arrays of type byte, boolean, double, long, short, char, float and Object. It looks like disjoint() method of Collections.
- static void fill(int array1, int num): Useful to fill the array array1 with the same element num. After this method call, all the elements are of the same value. The method is overloaded to take any data type and objects. It is similar to Collections.fill().
- static void fill(int array1, int startIndex, int endIndex, int num): It is a modified form of previous fill() method where a few elements of the array are filled with the same value; from startIndex to endIndex-1 in the array array1. The method is overloaded to accept arrays with different data types and objects. It is equivalent to Collections.fill().
- static List asList(T array): It is useful to convert an array into a List. The method returns a List object with the elements of the array and further elements cannot be added to the list. Useful to have fixed size list on which add() and remove() method do not work. It is nearer to addAll() of Collections. Overloaded to take any data type array as parameter.
- static int hashCode(int array1): Used to convert the array into hash code value. Hash code is useful to compare two arrays. It is the fastest way of comparing two arrays. Overloaded to take any data type array as parameter. Comes into existence with JDK 1.5.
- static String toString(int array1): Returns the array in string form useful to be displayed in a text field. This display the array elements comma-separated and placed within square brackets, "". Returns null if the array refers a null. This method eliminates the for loop to print the elements. Comes into existence with JDK 1.5.
The Arrays class from java.util package does not have copy() method to copy the elements of one array to another as arraycopy() method already exists with general arrays.