So I'm coding up a generic adjacency list and my code has no compile errors, but when I run my tests I get the same runtime error across the board:
java.lang.ClassCastException: [[Ljava.lang.Object; cannot be cast to [[Lds.Graph.Edge;
at ds.TheAdjacencyMatrix.AdjacencyMatrix.<init>(AdjacencyMatrix.java:86)
at ds.TheAdjacencyMatrix.AdjacencyMatrix.<init>(AdjacencyMatrix.java:63)
at ds.TheAdjacencyMatrix.AdjacencyMatrix.<init>(AdjacencyMatrix.java:73)
at ds.Graph.Test.TheAdjacencyMatrixTest.testAddVertex(TheAdjacencyMatrixTest.java:33)
The error is in the constructor on the line where I cast the 2d object array to E[][] type
The relevant code for the 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 that holds edge weights in weighted graphs.
* A 1 in a directed graph indicates an edge, a 0 indicates no edge.
*/
private E[][] matrix;
/**
* Array of elements contained in the graph.
* Elements correspond to the same indices as they do in the adjacency matrix of edges.
*
* i.e. matrix[4][5] is an edge from 4 to 5,
* elements[4] is the element at 4, elements[5] is the element at 5
*/
private T[] elements;
/**
* The maximum number of vertices in the adjacency matrix.
*/
private int size;
/**
* The current number of vertices in the graph.
*/
private int numVertices;
/**
* Indicates whether the graph is directed or not. True if directed, false otherwise.
*/
private boolean directed;
//~Constructors--------------------------------------------
/**
* Initializes the adjacency matrix to a size of 10.
* Which means there are 10 vertices in the graph.
*/
public AdjacencyMatrix() {
this(DEFAULT_SIZE);
}
/**
* Initializes the adjacency matrix to a size of 10. There will be 10 vertices in the graph.
*
* @param directed true if the graph is to be a directed graph, false otherwise.
*/
public AdjacencyMatrix(boolean directed) {
this();
this.directed = directed;
}
/**
* Initializes the adjacency matrix to a size of size.
* There will be a maximum size of *size* vertices in the graph
*
* @param size the size of the 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 the Edge class is an abstract class whose code is here:
package ds.Graph;
/**
* An abstract Edge class which has methods
* getWeight()
* and
* setWeight(int weight).
* Used for a Graph data structure to abstract
* out the edges.
*
*
*
*/
public abstract class Edge implements Comparable<Edge> {
/**
* Sets the weight of the edge to the passed in weight.
*
* @param weight the weight of the edge.
*/
public abstract void setWeight(int weight);
/**
* Gets the weight of the edge.
*
* @return the edge weight.
*/
public abstract int getWeight();
}
EDIT::
So this is the line of code that sets of the error at runtime. IntEdge is just an object inheriting from Edge that holds an integer.
AdjacencyMatrixInterface<String, IntEdge> matrix = new AdjacencyMatrix<String, IntEdge>(false);