Create new Array instance

ReturnMethodSummary
static Object newInstance(Class<?> componentType, int... dimensions) Creates a new array with the component type and dimensions.
static Object newInstance(Class<?> componentType, int length) Creates a new array with the component type and length.

import java.lang.reflect.Array;

public class Main {
  public static void main(String[] argv) throws Exception {
    int[] ints = (int[]) Array.newInstance(int.class, 10);
    
    String[] stringArray = (String[]) Array.newInstance(String.class, 10);
  }
}

Create multidimensional array


import java.lang.reflect.Array;

public class Main {
  public static void main(String[] argv) throws Exception {
    int[][] ints = (int[][]) Array.newInstance(int.class, 10,5);
    
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.