0

Hey this is my first post and i have this problem as you can see this the required method enter image description here

so i tried to create an array but an exception is showed so is there any suggestions ?

this my code

public class GArrayFactory {

// Create and return an array of size n

public static <T extends Comparable<T>> GArray<T>[] getGArray(int n) {

    GArray<T>[] array = (GArray<T>[]) new Object[n];

    return array ;

and this the exception

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LGArray;
at GArrayFactory.getGArray(GArrayFactory.java:9)
Faisal
  • 33
  • 4

1 Answers1

-1

You can create generic arrays using this method:

public class GArrayFactory<E> {

    private E[] array;

    public GArrayFactory(Class<E> clazz, int size) {
        final E[] array = (E[]) Array.newInstance(clazz, size);
        this.array = array;
    }

    public E[] getArray() {
        return array;
    }
}

Further explanation can be found here

Olantobi
  • 841
  • 1
  • 8
  • 16