Disallowed generic array creation in Java -
i thought understood not create arrays of generic classes in java few examples have seen lately have confused me. can explain? thanks!
i have generic class defined as: public class bag<item> implements iterable<item>
now in class called edgeweightedgraph (below), making array of bag , that's compiling fine. if bag wasn't generic class, know should have been fine here, is. why compiler not throwing error?
public class edgeweightedgraph { private final int v; private final bag[] adj; public edgeweightedgraph(int v) { this.v = v; adj = new bag[v]; } } while throwing error in generic class defined as:
public class minpq<key extends comparable<key>> { private key[] pq; public minpq() { pq = new key[2]; } }
in edgeweightedgraph, line not create generic array.
adj = new bag[v]; it creating array of raw type, allowed. bag class, not type parameter.
however, in minpq, key type parameter, not class, line attempts create generic array , compiler error.
pq = new key[2];
Comments
Post a Comment