Util.java Source code

Java tutorial

Introduction

Here is the source code for Util.java

Source

import java.lang.reflect.Array;
import java.util.Enumeration;
import java.util.Hashtable;

/*********************************************************************
* Array manipulation for Java 1.1+.
*
* <p>
* Java 1.1 compatible.
* </p>
*
* @see
*   ArrayLib2
*
* @version
*   2003-04-07
* @since
*   2001-04-06
* @author
*   <a href="http://croftsoft.com/">David Wallace Croft</a>*/
public class Util {

    /*********************************************************************
    * Creates a new subarray from a larger array.
    *
    * <p>
    * To avoid unnecessary object creation, this method returns the
    * original array argument if the requested subarray length is the same
    * and the startIndex is 0.  That is to say, if the method arguments
    * are such that the algorithm would have created a shallow clone, the
    * original array is returned instead.
    * </p>
    *
    * @throws NullArgumentException
    *
    *   If objectArray is null.
    *
    * @throws ArrayIndexOutOfBoundsException
    *
    *   If startIndex, length, or startIndex + length are out of range.
    *
    * @return
    *
    *   Returns an array with the same component type as the old array.
    *********************************************************************/
    public static Object[] subArray(Object[] objectArray, int startIndex, int length)
    //////////////////////////////////////////////////////////////////////
    {

        if ((startIndex == 0) && (length == objectArray.length)) {
            return objectArray;
        }

        Object[] newArray = (Object[]) Array.newInstance(objectArray.getClass().getComponentType(), length);

        System.arraycopy(objectArray, startIndex, newArray, 0, length);

        return newArray;
    }

}