Creating a generic array in Java
This post will discuss how to create a generic array in Java using an object array and Reflection Array class.
Arrays in Java contains information about their component type for allocating memory during runtime. Now, if the component type is not known at runtime, we cannot instantiate the array. Consider,
E[] arr = new E[capacity];
This uses generics. We know that generics are not present in the byte code generated by the compiler because of type erasure in Java. That means the type information is erased at the runtime, and new E[capacity] won’t know what type needs to be instantiated. To avoid this behavior, we should use List provided by the Java Collections Framework wherever we need generics.
But there are ways to create a generic array-like data structure in Java, which are covered below:
1. Using object array
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import java.util.Arrays; class Array<E> { private final Object[] arr; public final int length; // constructor public Array(int length) { // Creates a new object array of the specified length arr = new Object[length]; this.length = length; } // Method to get object present at index `i` in the array E get(int i) { @SuppressWarnings("unchecked") final E e = (E)arr[i]; return e; } // Method to set a value `e` at index `i` in the array void set(int i, E e) { arr[i] = e; } @Override public String toString() { return Arrays.toString(arr); } } class Main { // Program to create a generic array in Java public static void main(String[] args) { final int length = 5; // create an Integer array of the given length Array<Integer> intArray = new Array(length); for (int i = 0; i < length; i++) { intArray.set(i, i + 1); } System.out.println(intArray); // create a string array of the given length Array<String> strArray = new Array(length); for (int i = 0; i < length; i++) { strArray.set(i, String.valueOf((char)(i + 65))); } System.out.println(strArray); } } |
Output:
[1, 2, 3, 4, 5]
[A, B, C, D, E]
2. Using Reflection
We can use the Reflection Array class to create an array of a generic type known only at runtime. Please note that unlike the previous approach, we’re explicitly passing the Type information to the class constructor, which is further being passed to the Array.newInstance().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.Arrays; class Array<E> { private final E[] arr; public final int length; // constructor public Array(Class<E> type, int length) { // Creates a new array with the specified type and length at runtime this.arr = (E[]) java.lang.reflect.Array.newInstance(type, length); this.length = length; } // Method to get element present at index `i` in the array E get(int i) { return arr[i]; } // Method to set a value `e` at index `i` in the array void set(int i, E e) { arr[i] = e; } @Override public String toString() { return Arrays.toString(arr); } } class Main { // Program to create a generic array in Java public static void main(String[] args) { final int length = 5; // create an Integer array of the given length Array<Integer> intArray = new Array(Integer.class, length); for (int i = 0; i < length; i++) { intArray.set(i, i + 1); } System.out.println(intArray); // create a string array of the given length Array<String> strArray = new Array(String.class, length); for (int i = 0; i < length; i++) { strArray.set(i, String.valueOf((char)(i + 65))); } System.out.println(strArray); } } |
Output:
[1, 2, 3, 4, 5]
[A, B, C, D, E]
That’s all about creating a generic array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)