Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  Copyright 1999 CERN - European Organization for Nuclear Research.
  Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
  is hereby granted without fee, provided that the above copyright notice appear in all copies and 
  that both that copyright notice and this permission notice appear in supporting documentation. 
  CERN makes no representations about the suitability of this software for any purpose. 
  It is provided "as is" without expressed or implied warranty.
  */
  package org.apache.mahout.math;

Array manipulations; complements java.util.Arrays.

 
 public final class Arrays {
 
   private Arrays() {
   }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
 
   public static byte[] ensureCapacity(byte[] arrayint minCapacity) {
     int oldCapacity = array.length;
     byte[] newArray;
     if (minCapacity > oldCapacity) {
       int newCapacity = (oldCapacity * 3) / 2 + 1;
       if (newCapacity < minCapacity) {
         newCapacity = minCapacity;
       }
 
       newArray = new byte[newCapacity];
       System.arraycopy(array, 0, newArray, 0, oldCapacity);
     } else {
       newArray = array;
     }
     return newArray;
   }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
 
   public static char[] ensureCapacity(char[] arrayint minCapacity) {
     int oldCapacity = array.length;
     char[] newArray;
     if (minCapacity > oldCapacity) {
       int newCapacity = (oldCapacity * 3) / 2 + 1;
       if (newCapacity < minCapacity) {
         newCapacity = minCapacity;
       }
 
       newArray = new char[newCapacity];
       System.arraycopy(array, 0, newArray, 0, oldCapacity);
     } else {
       newArray = array;
     }
     return newArray;
   }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
 
   public static double[] ensureCapacity(double[] arrayint minCapacity) {
     int oldCapacity = array.length;
     double[] newArray;
     if (minCapacity > oldCapacity) {
       int newCapacity = (oldCapacity * 3) / 2 + 1;
       if (newCapacity < minCapacity) {
         newCapacity = minCapacity;
       }
 
       newArray = new double[newCapacity];
       //for (int i = oldCapacity; --i >= 0; ) newArray[i] = array[i];
       System.arraycopy(array, 0, newArray, 0, oldCapacity);
     } else {
       newArray = array;
     }
     return newArray;
  }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
  public static float[] ensureCapacity(float[] arrayint minCapacity) {
    int oldCapacity = array.length;
    float[] newArray;
    if (minCapacity > oldCapacity) {
      int newCapacity = (oldCapacity * 3) / 2 + 1;
      if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
      }
      newArray = new float[newCapacity];
      System.arraycopy(array, 0, newArray, 0, oldCapacity);
    } else {
      newArray = array;
    }
    return newArray;
  }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
  public static int[] ensureCapacity(int[] arrayint minCapacity) {
    int oldCapacity = array.length;
    int[] newArray;
    if (minCapacity > oldCapacity) {
      int newCapacity = (oldCapacity * 3) / 2 + 1;
      if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
      }
      newArray = new int[newCapacity];
      System.arraycopy(array, 0, newArray, 0, oldCapacity);
    } else {
      newArray = array;
    }
    return newArray;
  }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
  public static long[] ensureCapacity(long[] arrayint minCapacity) {
    int oldCapacity = array.length;
    long[] newArray;
    if (minCapacity > oldCapacity) {
      int newCapacity = (oldCapacity * 3) / 2 + 1;
      if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
      }
      newArray = new long[newCapacity];
      System.arraycopy(array, 0, newArray, 0, oldCapacity);
    } else {
      newArray = array;
    }
    return newArray;
  }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
  public static Object[] ensureCapacity(Object[] arrayint minCapacity) {
    int oldCapacity = array.length;
    Object[] newArray;
    if (minCapacity > oldCapacity) {
      int newCapacity = (oldCapacity * 3) / 2 + 1;
      if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
      }
      newArray = new Object[newCapacity];
      System.arraycopy(array, 0, newArray, 0, oldCapacity);
    } else {
      newArray = array;
    }
    return newArray;
  }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
  public static short[] ensureCapacity(short[] arrayint minCapacity) {
    int oldCapacity = array.length;
    short[] newArray;
    if (minCapacity > oldCapacity) {
      int newCapacity = (oldCapacity * 3) / 2 + 1;
      if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
      }
      newArray = new short[newCapacity];
      System.arraycopy(array, 0, newArray, 0, oldCapacity);
    } else {
      newArray = array;
    }
    return newArray;
  }

  
Ensures that a given array can hold up to minCapacity elements. Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new array with increased capacity containing the same elements, ensuring that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.
  public static boolean[] ensureCapacity(boolean[] arrayint minCapacity) {
    int oldCapacity = array.length;
    boolean[] newArray;
    if (minCapacity > oldCapacity) {
      int newCapacity = (oldCapacity * 3) / 2 + 1;
      if (newCapacity < minCapacity) {
        newCapacity = minCapacity;
      }
      newArray = new boolean[newCapacity];
      System.arraycopy(array, 0, newArray, 0, oldCapacity);
    } else {
      newArray = array;
    }
    return newArray;
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(byte[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(char[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(double[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(float[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(int[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(long[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(Object[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(short[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Returns a string representation of the specified array. The string representation consists of a list of the arrays's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

Returns:
a string representation of the specified array.
  public static String toString(boolean[] array) {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    int maxIndex = array.length - 1;
    for (int i = 0; i <= maxIndexi++) {
      buf.append(array[i]);
      if (i < maxIndex) {
        buf.append(", ");
      }
    }
    buf.append(']');
    return buf.toString();
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static byte[] trimToCapacity(byte[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      byte[] oldArray = array;
      array = new byte[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static char[] trimToCapacity(char[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      char[] oldArray = array;
      array = new char[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static double[] trimToCapacity(double[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      double[] oldArray = array;
      array = new double[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static float[] trimToCapacity(float[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      float[] oldArray = array;
      array = new float[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static int[] trimToCapacity(int[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      int[] oldArray = array;
      array = new int[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static long[] trimToCapacity(long[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      long[] oldArray = array;
      array = new long[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static Object[] trimToCapacity(Object[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      Object[] oldArray = array;
      array = new Object[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static short[] trimToCapacity(short[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      short[] oldArray = array;
      array = new short[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
Ensures that the specified array cannot hold more than maxCapacity elements. An application can use this operation to minimize array storage.

Returns the identical array if array.length <= maxCapacity. Otherwise, returns a new array with a length of maxCapacity containing the first maxCapacity elements of array.

Parameters:
maxCapacity the desired maximum capacity.
  public static boolean[] trimToCapacity(boolean[] arrayint maxCapacity) {
    if (array.length > maxCapacity) {
      boolean[] oldArray = array;
      array = new boolean[maxCapacity];
      System.arraycopy(oldArray, 0, array, 0, maxCapacity);
    }
    return array;
  }

  
  public static byte[] copyOf(byte[] srcint length) {
    byte[] result = new byte [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }
  
  
  public static char[] copyOf(char[] srcint length) {
    char[] result = new char [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }
  
  
  public static short[] copyOf(short[] srcint length) {
    short[] result = new short [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }
  
  
  public static int[] copyOf(int[] srcint length) {
    int[] result = new int [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }
  
  
  public static float[] copyOf(float[] srcint length) {
    float[] result = new float [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }

  
  public static double[] copyOf(double[] srcint length) {
    double[] result = new double [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }
  
  
  public static long[] copyOf(long[] srcint length) {
    long[] result = new long [length];
    System.arraycopy(src, 0, result, 0, Math.min(lengthsrc.length));
    return result;
  }
New to GrepCode? Check out our FAQ X