resize Array - Android java.lang

Android examples for java.lang:array resize

Description

resize Array

Demo Code


public class Main{

    public static Object resizeArray(Object oldArray, int newSize) {
        int oldSize = java.lang.reflect.Array.getLength(oldArray);

        @SuppressWarnings("rawtypes")
        Class elementType = oldArray.getClass().getComponentType();

        Object newArray = java.lang.reflect.Array.newInstance(elementType,
                newSize);//from w w  w  . j  av a  2  s  . com
        int preserveLength = Math.min(oldSize, newSize);

        if (preserveLength > 0)
            System.arraycopy(oldArray, 0, newArray, 0, preserveLength);

        return newArray;
    }

}

Related Tutorials