简体   繁体   中英

How do I add a null object to an ArrayList inside an ArrayList

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.

The initial size of graph is 0 , so the for loop in addVertex() runs one time less than it should:

public void addVertex(Vertex v) {
    verticies.add(v);
    graph.add(new ArrayList()); // graph now has size 1

    for (int i = 0; i < graph.size() - 1; i++) { // i = 0, 0 < 0 is false       
        graph.get(i).add(null); // this is not executed for the last added list
    }
}

The next time you call addVertex() it will add null to the previous ArrayList s, but not to the one you just added.

So you probably should do:

for (int i = 0; i < graph.size(); i++)

Even with this fix though, note that if you call addVertex() 5 times you will have something like this:

index             ArrayList
  0      [null, null, null, null, null]
  1      [null, null, null, null]
  2      [null, null, null]
  3      [null, null]
  4      [null]

This is probably not what you want. A better approach would be to add all your vertices first :

public void addVertex(Vertex v) {
    this.vertices.add(v);
}

And then create the ArrayList s for the adjacency matrix with the appropriate size:

public void initializeAdjacencyMatrix() {
    int n = this.vertices.size();
    for (int i = 0; i < n; i++) {
        List<Edge> edges = new ArrayList<>(Collections.nCopies(n, null));
        graph.add(edges);
    }
}

Also, you're using raw types when instantiating the ArrayList s. This is not a good practice. You should use the diamond operator instead. For example:

graph = new ArrayList<>();
graph.add(new ArrayList<>());

On this line:

for(int i=0; i<graph.size()-1; i++ ) {

remove the -1 . Because of the -1 when the size of the graph is one i will not be less than 0, so the loop does not run. If the loop does not run then your code which adds the null value can not run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM