Create new Array by size - Java Collection Framework

Java examples for Collection Framework:Array Length

Description

Create new Array by size

Demo Code


//package com.book2s;
import java.lang.reflect.Array;

public class Main {
    public static void main(String[] argv) {
        Class componentType = String.class;
        int newSize = 42;
        System.out.println(java.util.Arrays.toString(newArray(
                componentType, newSize)));
    }//from   www .j av  a  2s . co m

    @SuppressWarnings("unchecked")
    public static <T> T[] newArray(Class<?> componentType, int newSize) {
        return (T[]) Array.newInstance(componentType, newSize);
    }
}

Related Tutorials