-1

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++;
}
}
Elkin
  • 759
  • 1
  • 11
  • 23
  • 2
    What is type of `super.vertices`? – Andreas Feb 04 '16 at 20:51
  • Please make corrections - Constructor for DirectedGraph should be `public DirectedGraph()` not `public DiGraph()`. Additionally, the `ArrayList::addToRear(T t)` method does not exist. It sounds like you might want to use a `Deque`, which has a `addLast` method which might be doing what you want. – YoYo Feb 05 '16 at 05:37
  • Sorry. I just copied & pasted here and changed a little the code to simplify. The ArrayList::addToRear(T t) actually existed because i was using my own ArrayList implementation. Now i am using the Java's ArrayList but the problem still persists. Thanks for the warning. – Elkin Feb 05 '16 at 21:09

2 Answers2

1

You have limited T to subtypes of City, I am guessing that super.vertices is either return a super type of city or Object. By casting to T you are telling the compiler "trust me I know it's a subtype of City"

Gavin
  • 1,591
  • 15
  • 26
1

Please consider some of the corrections as posted as a comment on your Question.

I assume that the main body can look like something like this:

public static void main(String args[]) {
  new MyMap<City>().testing(new City());
}

Then, as to why 'casting to T fails':

When you instantiate new MyMap<City>(), it effectively tries to create an City [], but you assign it an Object []. The compiler allows this, because with Generics and Type erasure, nothing is wrong at this point. The compiler just doesn't know yet that it could get anything other than Object [] assigned.

A couple of solutions can be offered, all of them question if you shouldn't bring the design of the application back to the drawing board, and reconsider. However these are some of the options:

You will have to somehow pass along the type somehow. This can be accomplished by passing the T class, an instance of T, or a pre-existing array of T. I offer the second solution, which would make the initialization look as follows:

  public DirectedGraph(Class<? super T> class) {
      this.vertices = (T[]) Array.newInstance(clazz, DEFAULT_CAPACITY);
      this.edges = new double[DEFAULT_CAPACITY][DEFAULT_CAPACITY];
  }

Then for MyMap, as the default constructor in the parent is gone in DirectedGraph (we added a parameter), you will have to add one in MyMap instead:

  public MyMap() {
    super(City.class);
  }

With these code changes, I get just a step further. But because I have incomplete logic for the initialization, or because it has other issues a next unrelated error pops up.

I want to link following postings:

Community
  • 1
  • 1
YoYo
  • 7,796
  • 8
  • 50
  • 67
  • Thank you. It really works fine. You are a genius JoD. Thank you very much once again :) – Elkin Feb 05 '16 at 21:44