How to create a generic array in Java

Generic arrays in Java are described as arrays with parameterized types, allowing the arrays to operate on many data types while preserving each data type safety. Directly generating arrays with generic types is not supported in Java because of how arrays and generics are constructed and type erasure. We can bypass this restriction by making an array of objects and casting the components to the required generic type.

Implementation

@SuppressWarnings("unchecked")
//Define a class with generic type parameter
public class genericArray<T> {
//Declare an array
private T[] arr;
//Parameterized Constructor
public genericArray(int size) {
arr = (T[]) new Object[size];
}
//Getter & Setter
public void set(int index, T value) {
arr[index] = value;
}
public T get(int index) {
return arr[index];
}
//The main class
public static void main(String[] args) {
//array declaration
genericArray<Integer> array = new genericArray<>(4);
genericArray<String> array1 = new genericArray<>(2);
System.out.println("Array 1:");
array.set(0, 1);
array.set(1, 2);
array.set(2, 3);
array.set(3, 4);
System.out.println(array.get(0));
System.out.println(array.get(1));
System.out.println(array.get(2));
System.out.println(array.get(3));
System.out.println("----------------------------------------------");
System.out.println("Array 2:");
array1.set(0, "Hello");
array1.set(1, "Educative!!");
System.out.println(array1.get(0));
System.out.println(array1.get(1));
}
}
Implementation of generic array

Explanation

  • Line 1: Here, we have the @SuppressWarnings("unchecked") annotation to stop compiler warnings about unchecked type casting.

  • Line 4: This line defines a class genericArray with the generic type parameter T.

  • Line 7: This line declares an array to store generic elements.

  • Lines 10–12: Here, we have a parameterized constructor that initializes the array with a given size.

  • Lines 15–21: These lines define the getter and setter functions for the genericArray.

  • Lines 26–27: These lines create an instance genericArray with String and Integer as the generic types.

  • Lines 29–46: These lines use setter and getter to initialize and retrieve the arrays, and then print values from the arrays.

Limitations

This approach has some limitations and potential problems:

  1. Type cast safety: If the actual types do not match and the cast from Object[] to T[] is unchecked, it could result in runtime issues.

  2. Compiler warnings: Unchecked casting can cause compiler warnings and may not be the cleanest option regarding code quality.

Note: If possible, think about managing data types safely using Java’s built-in collections and data structures.

  1. Array reification: Type erasure, or removing type information at runtime, is how Java’s generics are implemented. This idea contradicts creating arrays with type information, which can lead to unexpected behavior.

  2. Alternative solutions: Using Java’s collection classes, such as ArrayList or other container classes, is sometimes advised when working with collections of generic types. These classes manage generic types more effectively and safely than manually managing arrays.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved