Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 谁能解释一下下面的for循环发生了什么?_Java_Arrays_For Loop_Graph - Fatal编程技术网

Java 谁能解释一下下面的for循环发生了什么?

Java 谁能解释一下下面的for循环发生了什么?,java,arrays,for-loop,graph,Java,Arrays,For Loop,Graph,我的预期产出是 4 vertices, 4 edges 0: 0 1 0 1 1: 1 0 1 0 2: 0 1 0 1 3: 1 0 1 0 它工作得很好,没有任何问题,但我不知道在下面的toString方法语句中的第二个for循环中发生了什么 有人能说出这个方法内部的情况吗,特别是在for循环中?? 以及为什么这个程序使用StringBuilder而不是字符串数据类型 这是我的完整图形程序 public class Graph { private int V;

我的预期产出是

4 vertices, 4 edges 

0: 0 1 0 1
1: 1 0 1 0 
2: 0 1 0 1 
3: 1 0 1 0 
它工作得很好,没有任何问题,但我不知道在下面的toString方法语句中的第二个for循环中发生了什么

有人能说出这个方法内部的情况吗,特别是在for循环中?? 以及为什么这个程序使用StringBuilder而不是字符串数据类型

这是我的完整图形程序

    public class Graph {

    private int V; // number of vertices in Graph
    private int E; // number of edges in Graph
    private int[][] adjMatrix;

    public Graph(int nodes) {
        this.V = nodes;
        this.E = 0;
        this.adjMatrix = new int[nodes][nodes];
    }

    public void addEdge(int u, int v) {
        adjMatrix[u][v] = 1;
        adjMatrix[v][u] = 1;  // because it is an undirected graph
        E++;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(V + " vertices, " + E + " edges " + "\n");
        for(int v = 0; v < V; v++) {
            sb.append(v + ": ");
            for(int w : adjMatrix[v]) {
                sb.append(w + " ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Graph g = new Graph(4);
        g.addEdge(0, 1);
        g.addEdge(1, 2);
        g.addEdge(2, 3);
        g.addEdge(3, 0);
        System.out.println(g.toString());
    }
}

对于二维数组adjMatrix中的每个整数,您在StringBuilder中追加它的值,实际上只是连接字符串。

您说它工作正常,但您问的是如何修复它?您不了解什么循环的哪一部分?
    public class Graph {

    private int V; // number of vertices in Graph
    private int E; // number of edges in Graph
    private int[][] adjMatrix;

    public Graph(int nodes) {
        this.V = nodes;
        this.E = 0;
        this.adjMatrix = new int[nodes][nodes];
    }

    public void addEdge(int u, int v) {
        adjMatrix[u][v] = 1;
        adjMatrix[v][u] = 1;  // because it is an undirected graph
        E++;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(V + " vertices, " + E + " edges " + "\n");
        for(int v = 0; v < V; v++) {
            sb.append(v + ": ");
            for(int w : adjMatrix[v]) {
                sb.append(w + " ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Graph g = new Graph(4);
        g.addEdge(0, 1);
        g.addEdge(1, 2);
        g.addEdge(2, 3);
        g.addEdge(3, 0);
        System.out.println(g.toString());
    }
}