bb.util
Class ArrayUtil

java.lang.Object
  extended by bb.util.ArrayUtil

public final class ArrayUtil
extends Object

Provides static utility methods that deal with arrays. (All these methods should have been put in java's Arrays class...)

This class is multithread safe: it is stateless.

Author:
Brent Boyer

Nested Class Summary
static class ArrayUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Constructor Summary
private ArrayUtil()
          This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.
 
Method Summary
static
<T> T[]
concatenate(T[] a1, T[] a2)
          This method concatenates two arrays into one.
static void shuffle(int[] a, Random random)
          Shuffles the elements of a in a random fashion.
static
<T> void
shuffle(T[] a)
          Simply calls shuffle(a, new Random()).
static
<T> void
shuffle(T[] a, Random random)
          Shuffles the elements of a in a random fashion.
static void swap(int[] a, int i, int j)
          Swaps elements i and j of a.
static
<T> void
swap(T[] a, int i, int j)
          Swaps elements i and j of a.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtil

private ArrayUtil()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.

Method Detail

concatenate

public static <T> T[] concatenate(T[] a1,
                                  T[] a2)
This method concatenates two arrays into one.

The concatenation is done is according to the following rules:

  1. if a1 == null, then a2 is returned (which could be null as well)
  2. else if a2 == null, then a1 is returned
  3. else if a1.length == 0, then a2 is returned (which could be zero length as well)
  4. else if a2.length == 0, then a1 is returned
  5. else a new array is allocated and the elements from a1 and then a2 are copied in order onto it before it is returned; note: the new array is created with a1's type, so a2 had better be compatible


shuffle

public static <T> void shuffle(T[] a)
                    throws IllegalArgumentException
Simply calls shuffle(a, new Random()).

Throws:
IllegalArgumentException - if a == null

shuffle

public static <T> void shuffle(T[] a,
                               Random random)
                    throws IllegalArgumentException
Shuffles the elements of a in a random fashion.

Throws:
IllegalArgumentException - if a == null; random == null

shuffle

public static void shuffle(int[] a,
                           Random random)
                    throws IllegalArgumentException
Shuffles the elements of a in a random fashion.

Throws:
IllegalArgumentException - if a == null; random == null

swap

public static <T> void swap(T[] a,
                            int i,
                            int j)
                 throws IllegalArgumentException,
                        ArrayIndexOutOfBoundsException
Swaps elements i and j of a.

Throws:
IllegalArgumentException - if a == null
ArrayIndexOutOfBoundsException - if i or j is an illegal index value for a

swap

public static void swap(int[] a,
                        int i,
                        int j)
                 throws IllegalArgumentException,
                        ArrayIndexOutOfBoundsException
Swaps elements i and j of a.

Throws:
IllegalArgumentException - if a == null
ArrayIndexOutOfBoundsException - if i or j is an illegal index value for a