Friday, November 13, 2015

Arrays

Don't reinvent the wheel when there is a racecar available for free!

I have the amusing difficulty of having been in Java too long. Back in version 1.0, I was writing my own serialization mechanism because none existed, however in version 1.1 Java serialization was introduced. In 1.1 and 1.2, I was working on the Voyager ORB where we were generating proxy classes on the fly. Lo and behold, in version 1.3 dynamic proxy generation was introduced.

The Java Core APIs and Java Extension APIs now have many capabilities that I am still realizing are there. I find that solving these small problems can be personally satisfying, but I still would rather have used one already available. I will document some of the useful Java Core APIs that I like to use.

Beginning with:

java.util.Arrays

The java.util.Arrays class has really grown up. Many methods are overloaded to support every primitive type as well as Java Generics parameterized Object references. In these cases, TYPE is used as a placeholder to indicate there is a method for every such type.

public static <T> List<T> asList(T... a)

Create a List front-end to an array. Any actions taken on items in the List affect the Objects in the underlying Array.


    public static void main (String [] args)
    {
        List<String> list = Arrays.asList(args);

    }

Since a java.util.List can only hold Object references and not primitive types, there are no primitive variants for this method.

public static boolean equals(TYPE [] a, TYPE [] a2)

Performs a shallow equals on the two arrays. For example, for primitive types the algorithm would be like:

return (a[0] == a2[0] && a[1] == a2[1] && ...)

whereas for Object types (assuming no null references):

return (a[0].equals(a2[0]) && a[1].equals(a2[1]) && .... )

public static boolean deepEquals(Object [] aObject [] a2)
public int deepHashCode(Object [] a)
public String deepToString(Object[] a)

The deep* methods recursively step into any array elements that are themselves arrays.

deepEquals() performs a deep equals on the two arrays. That is, if an element in the arrays is yet another array, then the contents of those arrays is recursively compared via deepEquals.

deepHashCode() calculates a hashCode value for the array by stepping into its contents and the contents of any arrays contained therein.

deepToString() creates a String value for the array from the String values of the array's contents and the contents of any arrays contained therein.

public static void sort (TYPE [] array, int fromIndex, int toIndex)
public static void sort (T[] array, Comparator<? super T> c)
public static void sort (T[] array, int fromIndx, int toIndex, Comparator<? super T> c)

The sort() methods perform an in-place quicksort on the given array. If the indices are given, then the sort is only performed between those indices. For Object references, a Comparator can be given. Once an array is sorted, the corresponding binarySearch() method can be used to efficiently search the data in the array.

Other methods include functions to copy part or all of the contents of an array and to fill an array. There area also methods to process the elements of an array in parallel for various behaviors including sort and set.

No comments:

Post a Comment