com.internationalnetwork.util
Class ArrayTools

Object
  extended by com.internationalnetwork.util.ArrayTools

public class ArrayTools
extends Object

Tools for dealing with arrays. Although many of these methods are trivial to develop, collecting them in a separate class can certainly lead to code that's easier to read.

Specific methods are created for each primitive type, so as to completely avoid causing any primitive-to-object-to-primitive overhead. Additionally all non-primitive types are supported without any need to utilize unsafe type-casting thanks to the behind-the-scenes magic of Java's Generics.


Field Summary
static String VERSION
          Version number of this Package (read-only).
 
Method Summary
static boolean contains(boolean[] array, boolean element)
          Determines if an array contains a specific element.
static boolean contains(byte[] array, byte element)
          Determines if an array contains a specific element.
static boolean contains(char[] array, char element)
          Determines if an array contains a specific element.
static boolean contains(double[] array, double element)
          Determines if an array contains a specific element.
static boolean contains(float[] array, float element)
          Determines if an array contains a specific element.
static boolean contains(int[] array, int element)
          Determines if an array contains a specific element.
static boolean contains(long[] array, long element)
          Determines if an array contains a specific element.
static boolean contains(short[] array, short element)
          Determines if an array contains a specific element.
static
<Type> boolean
contains(Type[] array, Type element)
          Determines if an array contains a specific element using the respective equals() method to perform the comparison.
static
<Type> HashMap<Type,Type>
createHashMap(Type... pairs)
          Creates a HashMap from an array of objects where both the Key and the Value are the same class (e.g., String).  This method makes it really easy to create a HashMap (a very nice aspect of Perl's syntax).
static String hex(boolean[] array)
          Converts an array of booleans into an unformatted hexadecimal string.
static String hex(byte[] array)
          Converts an array of bytes into an unformatted hexadecimal string.
static String hex(char[] array)
          Converts an array of characters into an unformatted hexadecimal string.
static String hex(double[] array)
          Converts an array of doubles into an unformatted hexadecimal string.
static String hex(float[] array)
          Converts an array of floats into an unformatted hexadecimal string.
static String hex(int[] array)
          Converts an array of integers into an unformatted hexadecimal string.
static String hex(long[] array)
          Converts an array of long integers into an unformatted hexadecimal string.
static String hex(short[] array)
          Converts an array of short integers into an unformatted hexadecimal string.
static
<Type> String
hex(Type[] array)
          Converts an array of objects into an unformatted hexadecimal string using the given object's built-in toString() method.
static int index(boolean[] array, boolean element)
          Identifies the first instance of an element in an array.
static int index(byte[] array, byte element)
          Identifies the first instance of an element in an array.
static int index(char[] array, char element)
          Identifies the first instance of an element in an array.
static int index(double[] array, double element)
          Identifies the first instance of an element in an array.
static int index(float[] array, float element)
          Identifies the first instance of an element in an array.
static int index(int[] array, int element)
          Identifies the first instance of an element in an array.
static int index(long[] array, long element)
          Identifies the first instance of an element in an array.
static int index(short[] array, short element)
          Identifies the first instance of an element in an array.
static
<Type> int
index(Type[] array, Type element)
          Identifies the first instance of an element in an array using the equals() method to perform the comparison.
static boolean[] join(boolean[]... array)
          Joins any number of arrays.
static byte[] join(byte[]... array)
          Joins any number of arrays.
static char[] join(char[]... array)
          Joins any number of arrays.
static double[] join(double[]... array)
          Joins any number of arrays.
static float[] join(float[]... array)
          Joins any number of arrays.
static int[] join(int[]... array)
          Joins any number of arrays.
static long[] join(long[]... array)
          Joins any number of arrays.
static short[] join(short[]... array)
          Joins any number of arrays.
static
<Type> Type[]
join(Type[]... array)
          Joins any number of arrays.  All NULL arrays are skipped to prevent a java.lang.NullPointerException exception.
static boolean max(boolean[] array)
          Returns the maximum value in an array.
static byte max(byte[] array)
          Returns the maximum value in an array.
static char max(char[] array)
          Returns the maximum value in an array.
static double max(double[] array)
          Returns the maximum value in an array.
static float max(float[] array)
          Returns the maximum value in an array.
static int max(int[] array)
          Returns the maximum value in an array.
static long max(long[] array)
          Returns the maximum value in an array.
static short max(short[] array)
          Returns the maximum value in an array.
static
<Type> Type
max(Type[] array)
          Returns the maximum HashCode value in an array.
static boolean min(boolean[] array)
          Returns the minimum value in an array.
static byte min(byte[] array)
          Returns the minimum value in an array.
static char min(char[] array)
          Returns the minimum value in an array.
static double min(double[] array)
          Returns the minimum value in an array.
static float min(float[] array)
          Returns the minimum value in an array.
static int min(int[] array)
          Returns the minimum value in an array.
static long min(long[] array)
          Returns the minimum value in an array.
static
<Type> Type
min(Type[] array)
          Returns the minimum value in an array.
static boolean[] removeNULLs(Boolean[] array)
          Returns a possibly shorter array with all NULL entries removed.
static byte[] removeNULLs(Byte[] array)
          Returns a possibly shorter array with all NULL entries removed.
static char[] removeNULLs(Character[] array)
          Returns a possibly shorter array with all NULL entries removed.
static double[] removeNULLs(Double[] array)
          Returns a possibly shorter array with all NULL entries removed.
static float[] removeNULLs(Float[] array)
          Returns a possibly shorter array with all NULL entries removed.
static int[] removeNULLs(Integer[] array)
          Returns a possibly shorter array with all NULL entries removed.
static long[] removeNULLs(Long[] array)
          Returns a possibly shorter array with all NULL entries removed.
static short[] removeNULLs(Short[] array)
          Returns a possibly shorter array with all NULL entries removed.
static
<Type> Type[]
removeNULLs(Type[] array)
          Returns a possibly shorter array with all NULL entries removed.
static boolean[] resize(boolean[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static byte[] resize(byte[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static char[] resize(char[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static double[] resize(double[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static float[] resize(float[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static int[] resize(int[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static long[] resize(long[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static short[] resize(short[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
static
<Type> Type[]
resize(Type[] array, int n)
          Constructs a new array based on the array provided, and with n elements.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Version number of this Package (read-only).

See Also:
Constant Field Values
Method Detail

contains

public static boolean contains(boolean[] array,
                               boolean element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(byte[] array,
                               byte element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(char[] array,
                               char element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(double[] array,
                               double element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(float[] array,
                               float element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(int[] array,
                               int element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(long[] array,
                               long element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static boolean contains(short[] array,
                               short element)
Determines if an array contains a specific element.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

contains

public static <Type> boolean contains(Type[] array,
                                      Type element)
Determines if an array contains a specific element using the respective equals() method to perform the comparison.

Parameters:
array - The array to search
element - The element to search for
Returns:
TRUE if the element was located, FALSE otherwise

createHashMap

public static <Type> HashMap<Type,Type> createHashMap(Type... pairs)
Creates a HashMap from an array of objects where both the Key and the Value are the same class (e.g., String).  This method makes it really easy to create a HashMap (a very nice aspect of Perl's syntax).

The following example code demonstrates how a static HashMap, containing a partial list of ISO country codes (the HashMap's "key") and corresponding country names (the HashMap's "value"), is defined (the "static" keyword is only needed because in this example the HashMap is being defined class-wide {outside of constructor and method declarations}):

      // --------------------------------------------------------------------------
      // Requires:  import com.internationalnetwork.util.ArrayTools;
      //   ...and:  import java.util.HashMap;
      // --------------------------------------------------------------------------
      static HashMap<String, String> COUNTRY_CODES = ArrayTools.createHashMap(
        "CA", "Canada",
        "CN", "China",
        "DE", "Germany",
        "UK', "United Kingdom",
        "US", "United States of America",
      );
    

The HashMap object is constructed with an initial capacity that is exactly half of the size of the array to reduce overhead.  If you provide duplicate keys (which is pointless because it wastes CPU cycles), then previously defined keys will be replaced.

The java.lang.UnsupportedOperationException exception is thrown if the array contains an odd number of objects.

Parameters:
pairs - Array that contains an even number of objects
Returns:
HashMap The new HashMap object

hex

public static String hex(boolean[] array)
Converts an array of booleans into an unformatted hexadecimal string.

It's important to note that each nybble (hexadecimal character) represents exactly 4 bits, or four boolean states. If you supply a number of booleans that is not a multiple of 4, then up to three "false" booleans will effectively be inserted so that the final boolean represents a value of 1.

For example, the following code...

System.out.println(ArrayTools.hex(true));
System.out.println(ArrayTools.hex(true, true));
System.out.println(ArrayTools.hex(true, true, true));
System.out.println(ArrayTools.hex(true, true, true, true));
System.out.println(ArrayTools.hex(true, true, true, true, true));
System.out.println(ArrayTools.hex(true, true, true, true, true, true));
System.out.println(ArrayTools.hex(true, true, true, true, true, true, true));
System.out.println(ArrayTools.hex(true, true, true, true, true, true, true, true));

...produces this output:

1
3
7
f
1f
3f
7f
ff

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied boolean[] array, in lower case, without any whitespace or other characters

hex

public static String hex(byte[] array)
Converts an array of bytes into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied byte[] array, in lower case, without any whitespace or other characters

hex

public static String hex(char[] array)
Converts an array of characters into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied char[] array, in lower case, without any whitespace or other characters

hex

public static String hex(double[] array)
Converts an array of doubles into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied double[] array, in lower case, without any whitespace or other characters

hex

public static String hex(float[] array)
Converts an array of floats into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied float[] array, in lower case, without any whitespace or other characters

hex

public static String hex(int[] array)
Converts an array of integers into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied int[] array, in lower case, without any whitespace or other characters

hex

public static String hex(long[] array)
Converts an array of long integers into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied long[] array, in lower case, without any whitespace or other characters

hex

public static String hex(short[] array)
Converts an array of short integers into an unformatted hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied short[] array, in lower case, without any whitespace or other characters

hex

public static <Type> String hex(Type[] array)
Converts an array of objects into an unformatted hexadecimal string using the given object's built-in toString() method. This might be useful with an array of String objects, or other similar objects, depending on the data stored within each object; it's important to keep in mind that when dealing with objects, the length of this particular data could vary, and there may be limits to the usefulness of the resulting hexadecimal string.

Parameters:
array - The array to convert
Returns:
Hexadecimal representation of the supplied short[] array, in lower case, without any whitespace or other characters

index

public static int index(boolean[] array,
                        boolean element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(byte[] array,
                        byte element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(char[] array,
                        char element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(double[] array,
                        double element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(float[] array,
                        float element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(int[] array,
                        int element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(long[] array,
                        long element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static int index(short[] array,
                        short element)
Identifies the first instance of an element in an array.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

index

public static <Type> int index(Type[] array,
                               Type element)
Identifies the first instance of an element in an array using the equals() method to perform the comparison.

Parameters:
array - The array to search
element - The element to search for
Returns:
int The index within the array of the element, or -1 if not found

join

public static boolean[] join(boolean[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static byte[] join(byte[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static char[] join(char[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static double[] join(double[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static float[] join(float[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static int[] join(int[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static long[] join(long[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static short[] join(short[]... array)
Joins any number of arrays.

Parameters:
array - The arrays to join
Returns:
the new array

join

public static <Type> Type[] join(Type[]... array)
Joins any number of arrays.  All NULL arrays are skipped to prevent a java.lang.NullPointerException exception.

Warning:  If the size of the target array is 0, then a NULL will be returned instead of a an empty array (this is due to a short-coming in the design of reflection and generics that comes into play when used with variable arguments {a.k.a., varargs}, and results in throwing an exception due to casting a two-dimensional array class to a one-dimensional array class).

Parameters:
array - The arrays to join
Returns:
the new array

max

public static boolean max(boolean[] array)
Returns the maximum value in an array.

For the purposes of this method, TRUE is greater than FALSE.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static byte max(byte[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static char max(char[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static double max(double[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static float max(float[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static int max(int[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static long max(long[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static short max(short[] array)
Returns the maximum value in an array.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

max

public static <Type> Type max(Type[] array)
Returns the maximum HashCode value in an array. We rely on the given Type's hashCode() method since there is no other known way to compare with the greater-than/less-than operators with non-primitive types.

Parameters:
array - The array to search
Returns:
the maximum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static boolean min(boolean[] array)
Returns the minimum value in an array.

For the purposes of this method, FALSE is less than TRUE.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static byte min(byte[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static char min(char[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static double min(double[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static float min(float[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static int min(int[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static long min(long[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

min

public static <Type> Type min(Type[] array)
Returns the minimum value in an array.

Parameters:
array - The array to search
Returns:
the minimum value
Throws:
ArrayIndexOutOfBoundsException - if an array containing 0 elements was supplied

removeNULLs

public static boolean[] removeNULLs(Boolean[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static byte[] removeNULLs(Byte[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static char[] removeNULLs(Character[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static double[] removeNULLs(Double[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static float[] removeNULLs(Float[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static int[] removeNULLs(Integer[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static long[] removeNULLs(Long[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static short[] removeNULLs(Short[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

removeNULLs

public static <Type> Type[] removeNULLs(Type[] array)
Returns a possibly shorter array with all NULL entries removed. If an empty array is provided, a new empty array will be created and returned.

Parameters:
array - The array to remove NULL entries from
Returns:
the possibly shorter array

resize

public static boolean[] resize(boolean[] array,
                               int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static byte[] resize(byte[] array,
                            int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static char[] resize(char[] array,
                            int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static double[] resize(double[] array,
                              int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static float[] resize(float[] array,
                             int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static int[] resize(int[] array,
                           int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static long[] resize(long[] array,
                            int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static short[] resize(short[] array,
                             int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements

resize

public static <Type> Type[] resize(Type[] array,
                                   int n)
Constructs a new array based on the array provided, and with n elements. If the new size is smaller, the highest numbered elements will always be truncated first.

Parameters:
array - The array to re-size
n - The desired size
Returns:
a new array containing n elements