danger.util
Class Arrays

java.lang.Object
  extended by danger.util.Arrays


public final class Arrays
extends Object

Array utilities.


Method Summary
static int binarySearch(int[] a, int key)
          Search the given presumed-sorted array for the given key.
static int binarySearch(Object[] a, Object key)
          Search the given presumed-sorted array for the given key, using Comparable.compareTo() with the key as an argument, as needed to perform a binary search.
static int binarySearch(Object[] a, Object key, Comparator comp)
          Search the given presumed-sorted array for the given key, using the given Comparator with the key as an argument, as needed to perform a binary search.
static void sort(int[] a)
          Sort the given array.
static void sort(int[] a, int fromIndex, int toIndex)
          Sort a part of the given array.
static void sort(Object[] a)
          Sort the given array using Comparable.compareTo() on elements as needed.
static void sort(Object[] a, Comparator comp)
          Sort the given array using the given Comparator to compare elements as needed.
static void sort(Object[] a, int fromIndex, int toIndex)
          Sort a part of the given array using Comparable.compareTo() on elements as needed.
static void sort(Object[] a, int fromIndex, int toIndex, Comparator comp)
          Sort a part of the given array using the given Comparator on elements as needed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sort

public static void sort(Object[] a)
Sort the given array using Comparable.compareTo() on elements as needed. This method uses the merge sort algorithm except on small arrays (for which it uses insertion sort), enhanced to provide near-O(N) performance on mostly-presorted data. The algorithm is stable, that is, equal elements will appear in the result in the same order that they appeared originally.

Parameters:
a - non-null; the array to sort

sort

public static void sort(Object[] a,
                        int fromIndex,
                        int toIndex)
Sort a part of the given array using Comparable.compareTo() on elements as needed. This method uses the merge sort algorithm except on small arrays (for which it uses insertion sort), enhanced to provide near-O(N) performance on mostly-presorted data. The algorithm is stable, that is, equal elements will appear in the result in the same order that they appeared originally.

Parameters:
a - non-null; the array to sort
fromIndex - the index of the first element to sort
toIndex - the index just past the last element to sort

sort

public static void sort(Object[] a,
                        Comparator comp)
Sort the given array using the given Comparator to compare elements as needed. This method uses the merge sort algorithm except on small arrays (for which it uses insertion sort), enhanced to provide near-O(N) performance on mostly-presorted data. The algorithm is stable, that is, equal elements will appear in the result in the same order that they appeared originally.

Parameters:
a - non-null; the array to sort
comp - null-ok; the comparator to use, or null to use Comparable.compareTo() instead of an explicit comparator

sort

public static void sort(Object[] a,
                        int fromIndex,
                        int toIndex,
                        Comparator comp)
Sort a part of the given array using the given Comparator on elements as needed. This method uses the merge sort algorithm except on small arrays (for which it uses insertion sort), enhanced to provide near-O(N) performance on mostly-presorted data. The algorithm is stable, that is, equal elements will appear in the result in the same order that they appeared originally.

Parameters:
a - non-null; the array to sort
fromIndex - the index of the first element to sort
toIndex - the index just past the last element to sort
comp - null-ok; the comparator to use, or null to use Comparable.compareTo() instead of an explicit comparator

sort

public static void sort(int[] a)
Sort the given array. This method uses the merge sort algorithm except on small arrays (for which it uses insertion sort), enhanced to provide near-O(N) performance on mostly-presorted data. The algorithm is stable, that is, equal elements will appear in the result in the same order that they appeared originally.

Parameters:
a - non-null; the array to sort

sort

public static void sort(int[] a,
                        int fromIndex,
                        int toIndex)
Sort a part of the given array. This method uses the merge sort algorithm except on small arrays (for which it uses insertion sort), enhanced to provide near-O(N) performance on mostly-presorted data.

Parameters:
a - non-null; the array to sort
fromIndex - the index of the first element to sort
toIndex - the index just past the last element to sort

binarySearch

public static int binarySearch(Object[] a,
                               Object key)
Search the given presumed-sorted array for the given key, using Comparable.compareTo() with the key as an argument, as needed to perform a binary search. Return the index where the key was found if it is found, or return ~indexToInsertAt if it was not found. If there is more than one element equal to the given key, then any such element may be indicated.

Parameters:
a - non-null; the array to search in
key - null-ok; the key to look for, which is always passed in as an argument to Comparable.compareTo()
Returns:
the index where the key is found if it is found, or ~insertionPoint if it was not found

binarySearch

public static int binarySearch(Object[] a,
                               Object key,
                               Comparator comp)
Search the given presumed-sorted array for the given key, using the given Comparator with the key as an argument, as needed to perform a binary search. Return the index where the key was found if it is found, or return ~indexToInsertAt if it was not found. If there is more than one element equal to the given key, then any such element may be indicated.

Parameters:
a - non-null; the array to search in
key - null-ok; the key to look for, which is always passed in as the second argument to Comparator.compare()
comp - null-ok; the comparator to use, or null to use Comparable.compareTo() instead of an explicit comparator
Returns:
the index where the key is found if it is found, or ~insertionPoint if it was not found

binarySearch

public static int binarySearch(int[] a,
                               int key)
Search the given presumed-sorted array for the given key. Return the index where the key was found if it is found, or return ~indexToInsertAt if it was not found. If there is more than one element equal to the given key, then any such element may be indicated.

Parameters:
a - non-null; the array in which to search
key - null-ok; the key to look for
Returns:
the index where the key is found if it is found, or ~insertionPoint if it was not found