Main Page

Previous Next

Utility Methods for Arrays

The Arrays class in java.util provides you with a set of static methods for operating on arrays. There are methods for sorting and searching arrays, as well as methods for comparing two arrays of elements of a basic type. You also have methods for filling arrays of elements with a given value. Let's look at the simplest method first, the fill() method for filling an array.

Filling an Array

The need to fill an array with a specific value arises quite often. The fill() method comes in a number of overloaded versions of the form:

fill(type [] array, type value)

Here type is a placeholder for the types supported by various versions of the method. The method stores value in each element of array. The return type is void so there is no return value. There are versions supporting type as any of the following:

boolean

byte

char

float

double

short

int

long

Object

 

The version of fill() accepting an array argument of type Object[] will obviously process an array of any class type.

Here's how you could fill an array of integers with a particular value:

long[] values = new long[1000];
java.util.Arrays.fill(values, 888L);  // Every element as 888

It's a relatively common requirement to fill an array of type char[] with spaces. Here's how you might use the fill() method in a method to put a value in a fixed width field:

// Return a value right justified in a field
public static String fixedWidth(double value, int width) {
  String valStr = String.valueOf(value);
  assert width> valStr.length(); 

  char[] spaces = new char[width-valStr.length()];
  java.util.Arrays.fill(spaces, ' ');                   // Fill array with blanks

  return new StringBuffer().append(spaces).append(valStr).toString();
}

This converts the value passed as the first argument to a string and checks that the field width specified is greater than the length of this string. The assertion ensures that we don't attempt to create an array with a negative number of elements. We then create an array of type char[] that will provide the spaces to the left of the value string. We fill this using the fill() method from the Arrays class in the java.util package. We then assemble the field as a StringBuffer object by appending the array followed by the value string and convert the result to type String before returning it. Clearly you could easily create an overloaded version of this method to output values of any basic type in a fixed width field. You could even embellish it with an option for left or right justification in the field.

There is a further form of fill() method that accepts four arguments. This is of the form:

fill(type[] array, int fromIndex, int toIndex, type value)

This will fill part of array with value, starting at array[fromIndex] up to and including array[toIndex-1]. There are versions of this method for the same range of types at the previous set of fill() methods. This variety of fill() will throw an exception of type IllegalArgumentException if fromIndex is greater than toIndex. It will also throw an exception of type ArrayIndexOutOfBoundsException if fromIndex is negative or toIndex is greater than array.length.

We could have taken a different approach to the fixedWidth() method and used this version of fill() in the process:

public static String fixedWidth(long value, int width) {
  char[] valChars = String.valueOf(value).toCharArray();
  assert width> valChars.length;         

  char[] field = new char[width];
  java.util.Arrays.fill(field, 0, width-valChars.length, ' ');  // Partial fill  
  with spaces   

  for(int i = 0 ; i<valChars.length ; i++)
    field[width-valChars.length+i] = valChars[i];      // Copy value characters                

  return new String(field);
}

Here we assemble the whole field as a character array. We fill the elements of the field array that will not be occupied by characters for the value with spaces. We then copy the characters from the array valChars to the back of the field array. Finally we return a String object that we create from the field array.

Comparing Arrays

There are nine overloaded versions of the equals() method for comparing arrays, one for each of the types that apply to the fill() method. All versions of equals() are of the form:

equals(type[] array1, type[] array2)

The method returns true if array1 is equal to array2 and false otherwise. The two arrays are equal if they contain the same number of elements and the values of all corresponding elements in the two arrays are equal. If array1 and array2 are both null, they are also considered to be equal.

When floating point arrays are compared, 0.0 is considered to be equal to -0.0, and two elements that contain NaN are also considered to be equal. When arrays with elements of a class type are compared, the elements are compared by calling the equals() method for the class. If you have not implemented the equals() method in your own classes then the version inherited from the Object class will be used. This compares references, not objects, and so only returns true if both object references refer to the same object.

Here's how you can compare two arrays:

String[] numbers = {"one", "two", "three", "four" };
String[] values = {"one", "two", "three", "four" };
if(java.util.Arrays.equals(numbers, values))
  System.out.println("The arrays are equal");
else
  System.out.println("The arrays are not equal");

In this fragment both arrays are equal so the equals() method will return true.

Sorting Arrays

The static sort() method in the Arrays class will sort the elements of an array passed as an argument into ascending sequence. The method is overloaded for eight of the nine types (boolean is excluded) we saw for the fill() method for each of two versions of sort():

sort(type[] array)

sort(type[] array, int fromIndex, int toIndex)

The first variety sorts the entire array into ascending sequence. The second variety sorts the elements from array[fromIndex] to array[toIndex-1] into ascending sequence. This will throw an exception of type IllegalArgumentException if fromIndex is greater than toIndex. It will also throw an exception of type ArrayIndexOutOfBoundsException if fromIndex is negative or toIndex is greater than array.length.

Obviously you can pass an array of elements of any class type to the versions of the sort() method that have the first parameter as type Object[]. If you are using either variety of the sort() method to sort objects then the class type of the objects must support the Comparable interface since the sort() method uses the compareTo() method to compare objects.

Here's how we can sort an array of strings:

String[] numbers = {"one", "two", "three", "four", "five",
                    "six", "seven", "eight"};
java.util.Arrays.sort(numbers);

After executing these statements the elements of the array numbers will contain:

"eight" "five" "four" "one" "seven" "six" "three" "two"

There are two further overloaded versions of sort() for sorting arrays of type Object[]. These are for sorting arrays where the order of elements is determined by using an external comparator object. The class type of the comparator object must implement the Comparator interface. One advantage of using an external comparator for sorting a set of objects is that you can have several comparators that can impose different orderings depending on the circumstances. For instance, in some cases you might want to sort a name file ordering by first name within second name. On other occasions you might want to sort by second name within first name. You can't do this using the Comparable interface implemented by the class. The sort() methods that make use of a comparator are:

sort(Object[] array, Comparator c)

sort(Object[] array, int fromIndex, int toIndex, Comparator c)

The Comparator interface declares two methods. The equals() method is used for comparing Comparator objects – the current Comparator object with another object of a type that also implements the Comparator interface. It returns a boolean value indicating whether the current object and the argument impose the same ordering on a set of objects. The compare() method in the Comparator interface is for comparing two objects identified by the two arguments of type Object. The method should return a negative integer, zero, or a positive integer when the first argument is less than, equal to, or greater than the second.

Searching Arrays

The binarySearch() method in the Arrays class will search the elements of a sorted array for a given value using the binary search algorithm. This only works if the elements of the array are in ascending sequence, so if they are not, you should call the sort() method to sort the array before calling the binarySearch() method. There are eight overloaded versions of the binarySearch() method of the form that we saw with the fill() method earlier. The boolean type is excluded from the nine types we saw, in this case as well.

binarySearch(type[] array, type value)

There is an additional version of the binarySearch() method for searching an array of type Object[] where you can supply a reference to a Comparator object as the fourth argument. All versions of the method return a value of type int, which is the index position in array where value was found. If the value is not in the array, a negative integer is returned that is produced by taking the value of the index position where the value would be if it was in the array, its sign reversing, and subtracting 1. For instance, suppose you have an array of integers containing the element values 2, 4, 6, 8, and 10:

int[] numbers = {2, 4, 6, 8, 10};

You could search for the value 7 with the statement:

int position = java.util.Arrays.binarySearch(numbers, 7);

The value of position will be -4, because if 7 was inserted in the array, it would be the fourth item in the array, or at index value 3 (remember that java arrays start at position zero). The return value is calculated as -3-1, which is -4. Thus if the value sought is not in the array then the return value is always negative.

Unless you are using a method that uses a comparator for searching arrays of objects, the class type of the array elements must implement the Comparable interface. Here's how we could search for a string in an array of strings:

String[] numbers ={"one", "two", "three", "four", "five", "six", "seven"};

java.util.Arrays.sort(numbers);
int position = java.util.Arrays.binarySearch(numbers, "three");

We have to sort the numbers array; otherwise the binary search won't work. After executing these statements the value of position will be 6.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor