resize an Array - Android java.lang

Android examples for java.lang:array resize

Description

resize an Array

Demo Code


//package com.java2s;

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  ava2s.  c o m*/
        int preserveLength = Math.min(oldSize, newSize);

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

        return newArray;
    }
}

Related Tutorials