Java Class New Instance newInstance(final Class type, final int length)

Here you can find the source of newInstance(final Class type, final int length)

Description

Create new array instance.
Throws:
- NullPointerException if the specified array type parameter is null;
- NegativeArraySizeException if the specified array length is negative.

License

Open Source License

Parameter

Parameter Description
type array type
length array length

Return

result array

Declaration

@SuppressWarnings("unchecked")
public static <T> T[] newInstance(final Class<T> type, final int length) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Array;

public class Main {
    /**//from www .  j a  va  2  s. c o m
     * Create new array instance.<br>
     * Throws:<br>
     * - {@link NullPointerException} if the specified array type parameter is <code>null</code>;<br>
     * - {@link NegativeArraySizeException} if the specified array length is negative.
     * 
     * @param type
     *            array type
     * @param length
     *            array length
     * @return result array
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] newInstance(final T type, final int length) {
        return (T[]) newInstance(type.getClass(), length);
    }

    /**
     * Create new array instance.<br>
     * Throws:<br>
     * - {@link NullPointerException} if the specified array type parameter is <code>null</code>;<br>
     * - {@link NegativeArraySizeException} if the specified array length is negative.
     * 
     * @param type
     *            array type
     * @param length
     *            array length
     * @return result array
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] newInstance(final Class<T> type, final int length) {
        return (T[]) Array.newInstance(type, length);
    }
}

Related

  1. newInstance(final Class clazz)
  2. newInstance(final Class clazz)
  3. newInstance(final Class clazz)
  4. newInstance(final Class clazz)
  5. newInstance(final Class clazz)
  6. newInstance(final ClassLoader classLoader, final String className, final Object... constructorArgs)
  7. newInstance(final Object obj, final String clazz)
  8. newInstance(final String className, ClassLoader cl)
  9. newInstance(final String className, final Object[] args)