weiss.util
Class Arrays

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

public class Arrays
extends java.lang.Object

Instanceless class that contains static methods to manipulate arrays.


Method Summary
static
<AnyType extends java.lang.Comparable<? super AnyType>>
int
binarySearch(AnyType[] arr, AnyType x)
          Performs a search on sorted array arr.
static
<AnyType> int
binarySearch(AnyType[] arr, AnyType x, Comparator<? super AnyType> cmp)
          Performs a search on sorted array arr using a comparator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

binarySearch

public static <AnyType extends java.lang.Comparable<? super AnyType>> int binarySearch(AnyType[] arr,
                                                                                       AnyType x)
Performs a search on sorted array arr. If arr is not sorted, results are undefined. Implicitly, this assumes that all objects are Comparable.

Parameters:
arr - the array to search.
x - the object to search for.
Returns:
if x is found, returns the index where it is found; otherwise, x is not found. In that case, a negative number is always returned, and this number is equal to -( p + 1 ), where p is the first position greater than x. This can range from -1 down to -(arr.length+1).
Throws:
java.lang.ClassCastException - if comparsions cannot be performed.

binarySearch

public static <AnyType> int binarySearch(AnyType[] arr,
                                         AnyType x,
                                         Comparator<? super AnyType> cmp)
Performs a search on sorted array arr using a comparator. If arr is not sorted, results are undefined.

Parameters:
arr - the array to search.
x - the object to search for.
cmp - the comparator.
Returns:
if x is found, returns the index where it is found. otherwise, x is not found. In that case, a negative number is always returned, and this number is equal to -( p + 1 ), which p is the first position greater than x. This can range from -1 down to -(arr.length+1).
Throws:
java.lang.ClassCastException - if comparsions cannot be performed.