1

Hi I'm doing revision for an upcoming test and I've hit a problem I'm having trouble with.

I have to modify this code http://pastebin.com/ED2A7VWy to give a generic implementation of Queue. The problem is that the Queue uses an array and for some reason the generics don't seem to play nicely with arrays. I've tried:

public class Queue<E>{
    private E[] eArray = new E[5];
...

}

but that doesn't seem to work.

user2320239
  • 873
  • 2
  • 13
  • 33

2 Answers2

2

Cast the array instead :

@SuppressWarnings("unchecked")
private E[] eArray = (E[])new Object[5];

You can read here why it is not allowed.

Community
  • 1
  • 1
user2336315
  • 14,237
  • 9
  • 37
  • 63
-2

You can do this - no suppressing of warnings - no casting:

public class Objects {

    // Call without a second parameter to get an array of the specified type with the specified length.
    public static <T> T[] newArray(int length, T... empty) {
        return Arrays.copyOfRange(empty, 0, length);
    }
}

public class Test<T> {

    public void test() {
        // Only specify the length - Java creates an empty one for the varargs param.
        T[] demo = Objects.<T>newArray(5);
        System.out.println(Arrays.toString(demo));
    }
OldCurmudgeon
  • 60,862
  • 15
  • 108
  • 197
  • how is the relevant to the question? You can also do `String[] strings = new String[5];` – newacct Jan 10 '14 at 10:04
  • @newacct - it is a way of creating an array of a generic type without casting or suppressing warnings. – OldCurmudgeon Jan 10 '14 at 10:13
  • It does not create an array of a generic type. You are misunderstanding what it does. A `String[]` is created in `main` and passed in, and the method simply creates another array of the same runtime type. – newacct Jan 10 '14 at 10:22
  • @newacct - It can be used to create an array of any type - including generic types - without casting or suppressing warnings. To me that has value. – OldCurmudgeon Jan 10 '14 at 10:44
  • It does not create an array of generic types. You still need to pass an already created array of the correct runtime type for it to create it. It has no value. In your case you have to explicitly specify `String` to call it. – newacct Jan 10 '14 at 10:46
  • @newacct - if you leave out the varargs parameter completely an empty one is created for you. Example code edited to clarify. – OldCurmudgeon Jan 10 '14 at 10:52
  • `Objects.newArray(5)` is actually the same as `new Object[5]`. It does not create an array of `T`. – newacct Jan 10 '14 at 18:43
  • @newacct - Correct - but you would have to cast a `new Object[5]`. – OldCurmudgeon Jan 10 '14 at 19:53
  • So? You have an implicit unsafe "cast" that you can't see. `Objects.newArray(5)` does the exact same as `(T[])new Object[5]`. Neither creates an array of a generic type. Both are unsafe. Calls to your `Objects.newArray` method can *always* be replaced by `new Something[]`. It does not accomplish anything new or different, but is a more convoluted way to write `new Something[]`. – newacct Jan 10 '14 at 20:02