Java Generics Causing Class Cast Error -


so i'm coding generic adjacency list , code has no compile errors, when run tests same runtime error across board:

java.lang.classcastexception: [[ljava.lang.object; cannot cast [[lds.graph.edge;     @ ds.theadjacencymatrix.adjacencymatrix.<init>(adjacencymatrix.java:86)     @ ds.theadjacencymatrix.adjacencymatrix.<init>(adjacencymatrix.java:63)     @ ds.theadjacencymatrix.adjacencymatrix.<init>(adjacencymatrix.java:73)     @ ds.graph.test.theadjacencymatrixtest.testaddvertex(theadjacencymatrixtest.java:33) 

the error in constructor on line cast 2d object array e[][] type

the relevant code adjacency matrix is::

public class adjacencymatrix<t, e extends edge>          implements adjacencymatrixinterface<t, e>, graph<t, e> {      //~constants----------------------------------------------     private static final int default_size = 10;      //~data fields--------------------------------------------     /**      * int matrix holds edge weights in weighted graphs.       * 1 in directed graph indicates edge, 0 indicates no edge.      */     private e[][] matrix;      /**      * array of elements contained in graph.      * elements correspond same indices in adjacency matrix of edges.      *       * i.e. matrix[4][5] edge 4 5,       *  elements[4] element @ 4, elements[5] element @ 5      */     private t[] elements;      /**      * maximum number of vertices in adjacency matrix.      */     private int size;      /**      * current number of vertices in graph.      */     private int numvertices;      /**      * indicates whether graph directed or not. true if directed, false otherwise.      */     private boolean directed;      //~constructors--------------------------------------------     /**      * initializes adjacency matrix size of 10.      * means there 10 vertices in graph.      */     public adjacencymatrix() {          this(default_size);     }      /**      * initializes adjacency matrix size of 10. there 10 vertices in graph.      *       * @param directed true if graph directed graph, false otherwise.      */     public adjacencymatrix(boolean directed) {          this();         this.directed = directed;     }      /**      * initializes adjacency matrix size of size.      * there maximum size of *size* vertices in graph      *       * @param size size of adjacency matrix.      */     @suppresswarnings("unchecked")     public adjacencymatrix(int size) {          matrix = (e[][]) new object[size][size];         elements = (t[]) new object[size];          this.size = size;         numvertices = 0;         directed = false;     } 

and edge class abstract class code here:

package ds.graph;  /**  * abstract edge class has methods  * getweight()  * ,  * setweight(int weight).  * used graph data structure abstract  * out edges.  *   *  *  */ public abstract class edge implements comparable<edge> {      /**      * sets weight of edge passed in weight.      *       * @param weight weight of edge.      */     public abstract void setweight(int weight);      /**      * gets weight of edge.      *       * @return edge weight.      */     public abstract int getweight(); } 

edit::

so line of code sets of error @ runtime. intedge object inheriting edge holds integer.

adjacencymatrixinterface<string, intedge> matrix = new adjacencymatrix<string, intedge>(false); 

simple change line to

matrix = (e[][]) new edge[size][size]; 

e erased upper bound inside class. e's upper bound edge in case. try cast edge[][].

also, have make sure matrix , elements not exposed outside e[][] , t[] respectively, since not of types. long use them within class, there no problem.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -