Resizes an array. - Java Collection Framework

Java examples for Collection Framework:Array Length

Description

Resizes an array.

Demo Code

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.
import java.lang.reflect.Array;
import static jodd.util.StringPool.NULL;

public class Main{
    /**/*from   ww w .j  a  v  a2  s .c o m*/
     * Resizes an array.
     */
    public static <T> T[] resize(T[] buffer, int newSize) {
        Class<T> componentType = (Class<T>) buffer.getClass()
                .getComponentType();
        T[] temp = (T[]) Array.newInstance(componentType, newSize);
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>String</code> array.
     */
    public static String[] resize(String buffer[], int newSize) {
        String temp[] = new String[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>byte</code> array.
     */
    public static byte[] resize(byte buffer[], int newSize) {
        byte temp[] = new byte[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>char</code> array.
     */
    public static char[] resize(char buffer[], int newSize) {
        char temp[] = new char[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>short</code> array.
     */
    public static short[] resize(short buffer[], int newSize) {
        short temp[] = new short[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>int</code> array.
     */
    public static int[] resize(int buffer[], int newSize) {
        int temp[] = new int[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>long</code> array.
     */
    public static long[] resize(long buffer[], int newSize) {
        long temp[] = new long[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>float</code> array.
     */
    public static float[] resize(float buffer[], int newSize) {
        float temp[] = new float[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>double</code> array.
     */
    public static double[] resize(double buffer[], int newSize) {
        double temp[] = new double[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>boolean</code> array.
     */
    public static boolean[] resize(boolean buffer[], int newSize) {
        boolean temp[] = new boolean[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
}

Related Tutorials