Java Create New Array with Class Type and Length

Tags: Array

In this Java core tutorial we learn how to use the java.lang.reflect.Array class to create a new array with specified class type and length in Java programming language.

How to use Array.newInstance() method

In Java we can use the Array.newInstance(Class<?> componentType, int length) method to create a new instance of array with given class type and array length as the following example Java code.

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayNewInstanceExample1 {
    public static void main(String... args) {
        int arrayLength = 10;

        // Create a new array of String objects
        String[] arrayOfString = (String[])Array.newInstance(String.class, arrayLength);
        // Create a new array of Integer objects
        Integer[] arrayOfInteger = (Integer[])Array.newInstance(Integer.class, arrayLength);

        // Set values to the arrays
        for(int i = 0; i < arrayLength; i++) {
            Array.set(arrayOfString, i, "Value " + (i + 1));
            Array.set(arrayOfInteger, i, (i + 1));

        System.out.println("Array of Strings:");
        System.out.println("Array of Integers:");
The output as below.
Array of Strings:
[Value 1, Value 2, Value 3, Value 4, Value 5, Value 6, Value 7, Value 8, Value 9, Value 10]
Array of Integers:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Happy Coding 😊

Java Create ArrayList using List Interface

Java Convert ArrayList to Array

Java Convert ArrayList to String

Java Convert ArrayList to Comma Separated String

Java Convert ArrayList to HashSet

Java Convert ArrayList to LinkedList

Convert Array to List in Java

Java Get Index of Minimum Value in ArrayList

How to use Java ArrayList with Examples

Java Convert Array to ArrayList

Java Insert Element to ArrayList at Specified Index

Java Get Minimum Value in ArrayList

How to Traverse ArrayList using Iterator in Java

How to Traverse ArrayList using for each loop in Java

How to Traverse ArrayList using forEach() method in Java

Java Add Element to ArrayList