I thought I understood that you could not create arrays of generic classes in Java but a few examples I have seen lately have really confused me. Can someone explain? Thanks!
I have a generic class defined as: public class Bag<Item> implements Iterable<Item>
Now in a class called EdgeWeightedGraph (below), I am making an array of Bag and that's compiling just fine. If Bag wasn't a generic class, I know that this should have been fine but here, it is. Then why is the compiler not throwing an error?
public class EdgeWeightedGraph {
private final int V;
private final Bag[] adj;
public EdgeWeightedGraph(int V) {
this.V = V;
adj = new Bag[V];
}
}
While it is throwing an error in another generic class defined as:
public class MinPQ<Key extends Comparable<Key>> {
private Key[] pq;
public MinPQ() {
pq = new Key[2];
}
}