Programming Tutorials

Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals()

By: Fazal in Java Tutorials on 2007-09-14  

Java 2 added a new class to java.util called Arrays. This class provides various methods that are useful when working with arrays. Although these methods technically aren't part of the collections framework, they help bridge the gap between collections and arrays. Each method defined by Arrays is examined in this tutorial.

The asList() method returns a List that is backed by a specified array. In other words, both the list and the array refer to the same location. It has the following signature:

static List asList(Object[ ] array)

Here, array is the array that contains the data.

The binarySearch() method uses a binary search to find a specified value. This method must be applied to sorted arrays. It has the following forms:

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 int binarySearch(Object[ ] array, Object value, Comparator c)

Here, array is the array to be searched and value is the value to be located. The last two forms throw a 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, the Comparator c is used to determine the order of the elements in array. In all cases, if value exists in array, the index of the element is returned. Otherwise, a negative value is returned.

The equals() method returns true if two arrays are equivalent. Otherwise, it returns false. The equals() method has the following forms:

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[])

Here, array1 and array2 are the two arrays that are compared for equality.

The fill() method assigns a value to all elements in an array. In other words, it fills an array with a specified value. The fill() method has two versions. The first version, which has the following forms, fills an entire array:

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)
Here, value is assigned to all elements in array.

The second version of the fill() method assigns a value to a subset of an array. Its forms are shown here:

static void fill(boolean array[ ], int start, int end,boolean value)
static void fill(byte array[ ], int start, int end,byte value)
static void fill(char array[ ], int start, int end,char value)
static void fill(double array[ ], int start, int end,double value)
static void fill(float array[ ], int start, int end,float value)
static void fill(int array[ ], int start, int end,int value)
static void fill(long array[ ], int start, int end,long value)
static void fill(short array[ ], int start, int end,short value)
static void fill(Object array[ ], int start, int end,Object value)

Here, value is assigned to the elements in array from position start to position end-1. These methods may all throw an IllegalArgumentException if start is greater than end, or an ArrayIndexOutOfBoundsException if start or end is out of bounds.

The sort() method sorts an array so that it is arranged in ascending order. The sort() method has two versions. The first version, shown here, sorts the entire array:

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 void sort(Object array[ ], Comparator c)

Here, array is the array to be sorted. In the last form, c is a Comparator that is used to order the elements of array. The forms that sort arrays of Object can also throw a ClassCastException if elements of the array being sorted are not comparable. The second version of sort() enables you to specify a range within an array that you want to sort. Its forms are shown here:

static void sort(byte array[ ], int start, int end)
static void sort(char array[ ], int start, int end)
static void sort(double array[ ], int start, int end)
static void sort(float array[ ], int start, int end)
static void sort(int array[ ], int start, int end)
static void sort(long array[ ], int start, int end)
static void sort(short array[ ], int start, int end)
static void sort(Object array[ ], int start, int end)
static void sort(Object array[ ], int start, int end, Comparator c)

Here, the range beginning at start and running through end-1 within array will be sorted. In the last form, c is a Comparator that is used to order the elements of array. All of these methods can throw an IllegalArgumentException if startis greater than end, or an ArrayIndexOutOfBoundsException if start or endis out of bounds. The last two forms can also throw a ClassCastException if elements of the array being sorted are not comparable.

The following program illustrates how to use some of the methods of the Arrays class:

// Demonstrate Arrays
import java.util.*;

class ArraysDemo {
    public static void main(String args[]) {
        // allocate and initialize array
        int array[] = new int[10];
        for (int i = 0; i < 10; i++)
            array[i] = -3 * i;
        // display, sort, display
        System.out.print("Original contents: ");
        display(array);
        Arrays.sort(array);
        System.out.print("Sorted: ");
        display(array);
        // fill and display
        Arrays.fill(array, 2, 6, -1);
        System.out.print("After fill(): ");
        display(array);
        // sort and display
        Arrays.sort(array);
        System.out.print("After sorting again: ");
        display(array);
        // binary search for -9
        System.out.print("The value -9 is at location ");
        int index = Arrays.binarySearch(array, -9);
        System.out.println(index);
    }

    static void display(int array[]) {
        for (int i = 0; i < array.length; i++)
            System.out.print(array[i] + " ");
        System.out.println("");
    }
}

The following is the output from this program:

Original contents: 0 -3 -6 -9 -12 -15 -18 -21 -24 -27
Sorted: -27 -24 -21 -18 -15 -12 -9 -6 -3 0
After fill(): -27 -24 -1 -1 -1 -1 -9 -6 -3 0
After sorting again: -27 -24 -9 -6 -3 -1 -1 -1 -1 0
The value -9 is at location 2





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)