0

I have a generic class, called Node<E> and I'm trying to define a static method that will return an array of objects of this type. I understand that Java does not allow the creation of generic type arrays within a class so we have to create an Object array and pass our generic types through the object.

For an example of the code, let's define it as:

public static Object[] returnArray() {
    Object[] output;
    //code which adds Node<E> to output
    return output;

When I implement this code though, I am not able to:

  • perform this class's methods on the objects within the array as they are of type Object
    • If Node<E> had a method .getVal() the following code results in an error:
      • Object[] nodes = Node.returnArray(head); nodes[0].getVal();
  • cast and return method array to a Node<E>[] array, even when explicit of the type.
    • Node<Integer>[] nodes = (Node<Integer>[]) Node.returnArray(head);

So the only way I am currently accessing the methods of each node is by assigning them to a variable by casting:

Object[] nodes = Node.returnArray(head);
Node<Integer> returned1 = (Node<Integer>) nodes[0];
Node<Integer> returned2 = (Node<Integer>) nodes[1];

But you can see how redundant it gets as the code grows. Is there a better way to access generic objects passed to an array?

Turing85
  • 13,364
  • 5
  • 27
  • 49
flatline_
  • 15
  • 2
  • 3
    You can't make an array of `E`, or of `Node`, but you can certainly make an array of `Node`. – user2357112 supports Monica Nov 05 '17 at 19:09
  • 5
    Also, you should probably just make a List instead. No point dealing with the headaches of trying to get generics and arrays to work together if you can just use a List. – user2357112 supports Monica Nov 05 '17 at 19:11
  • 1
    you can create an array of `Node` and cast it to `Node[]`, and add warning suppression to your method. sure it's now your disposal to make sure it's indeed `E`(which in many cases wouldn't be hard). – Jason Hu Nov 05 '17 at 19:22
  • Possible duplicate of [How to create a generic array in Java?](https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – user1803551 Nov 05 '17 at 20:02

1 Answers1

0

You can create generic arrays. You just need the Class object for that. Consider this overly complicated program for printing the number 6:

public class GenericClass<T> {

    T[] array;

    public GenericClass(int length, Class<T> clazz) {
        array = (T[]) java.lang.reflect.Array.newInstance(clazz, length);
    }

    public T get(int i) {
        return array[i];
    }

    public T set(int i, T val) {
        return array[i] = val;
    }

    public static void main(String... args) {
        GenericClass<Integer> example = new GenericClass<>(5, Integer.class);
        example.set(0, 6);
        System.out.println(example.get(0)); //Prints 6
    }
}

This way, the array you create is really of type T[], not Object[], so it's safe to use and even externalize. Another option, of course, is just to use a list.

Malt
  • 25,324
  • 9
  • 56
  • 86
  • That's not relevant here. They are not trying to create an array of a type variable type. The runtime component type of the array here *is* known at compile time -- it is `Node`. – newacct Nov 21 '17 at 17:24