The code below on the first commented line says "City cannot be converted to T". So on the next line, i casted it to T. After compiling the method "testing" in the main program it throws this exception: "Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lapi.xapi.City;"
Can anyone explain why this happens and get a solution if possible?
Thanks in advance.
By the way, i need to acess some methods from City class in MyMap class and i can only use casts if there's really no other way.
public class MyMap <T extends City> extends DirectedGraph<City> {
public void add(T vertex) {
super.addVertex(vertex);
}
public void testing(T vertex) {
ArrayList<T> x = new ArrayList<>();
//x.add(super.vertices[0]);
x.add((T) super.vertices[0]);
}
}
------------------------------------------------------------------------------------------
Info Update: Sorry for the lack of info.
This is superclass DirectedGraph:
public class DirectedGraph<T>{
protected final int DEFAULT_CAPACITY=15;
protected T[] vertices;
protected double[][] edges;
protected int numVertices;
public DirectedGraph() {
this.vertices = (T[]) (new Object[DEFAULT_CAPACITY]);
this.edges = new double[DEFAULT_CAPACITY][DEFAULT_CAPACITY];
}
public void addVertex(T vertex) {
this.vertices[this.numVertices] = vertex;
this.numVertices++;
}
}