I am trying to represent an Ad Hoc network using the adjacency matrix structure. To do this, I am creating an ArrayList inside another ArrayList.
When I add a new vertex to the graph, I create a new ArrayList (inside a super ArrayList) and I then have a loop to add a new null object to each ArrayList, however the size of the ArrayLists do not increase correctly and I can't figure out why.
Here is my code:
public class Matrix {
public ArrayList<ArrayList<Edge>> graph;
public ArrayList<Vertex> verticies;
public ArrayList<Edge> edges;
public Matrix() {
graph = new ArrayList();
verticies = new ArrayList();
edges = new ArrayList();
}
public Matrix(ArrayList<Vertex> verticies, ArrayList<Edge> edges) {
this.verticies = verticies;
this.edges = edges;
}
public void addVertex(Vertex v) {
verticies.add(v);
graph.add(new ArrayList());
for(int i=0; i<graph.size()-1; i++ ) {
graph.get(i).add(null);
}
}
Any help would be greatly appreciated.