java - 图矩阵项目(静态中的非静态)

标签 java graph matrix

我有一个任务是编写一个图形矩阵,该矩阵采用数字标记它们并吐出连接。现在有这个非静态/静态东西的基本问题。尽管我认为我理解类和该类的实例之间的区别,但我不明白这个问题。当我运行这个时,首先出现一个问题 for 循环。不会因输入标签而暂停。我感谢任何帮助和/或批评。

public class GraphMatrix {

class Matrix {

    private int matrix[][];
    private int size;
    private String labels[];

    private void createMatrix() {
        Scanner in = new Scanner(System.in);
        System.out.println("How many points will be represented in this graph?");
        size = in.nextInt();
        matrix = new int[size][size];
        labels = new String[size];
        System.out.println("Please label each point.");
        for (int i = 1; i <= size; i++) {
            System.out.println("Enter label for point #" + (i));
            String key = in.nextLine();
            labels[i] = key;
        }
        System.out.println("Please define edges between points or enter -1 when finished:");
        int finish = 1;
        while (finish == 1) {
            int jkey = 0;
            int kkey = 0;
            int count = 0;
            boolean pass = false;
            System.out.println("Point labeled:");
            String j = in.nextLine();
            while (pass = false) {
                if (labels[count].equals(j)) {
                    jkey = count;
                    count = 0;
                    pass = true;
                }
            }
            System.out.println("to point labeled:");
            String k = in.nextLine();
            while (pass = true) {
                if (labels[count].equals(j)) {
                    kkey = count;
                    pass = false;
                }
            }
            matrix[jkey][kkey] = 1;
            System.out.println("Finished enter -1, to define more connections enter 1");
            finish = in.nextInt();
        }

    }

    private void listEdges() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (matrix[i][j] == 1) {
                    System.out.println("There is an edge between" + labels[i] + " and" + labels[j]);
                }
            }
        }
    }
}

public static void main(String[] args) {
    Matrix neo = new Matrix();
    neo.createMatrix();
    neo.listEdges();
}

}

最佳答案

您需要对 ma​​in 方法进行如下更改以消除编译器错误。

来自

 Matrix neo = new Matrix();

  GraphMatrix graphMatrix = new GraphMatrix();
    Matrix neo = graphMatrix.new Matrix();

注意:要实例化内部类,必须先实例化外部类。然后,使用以下语法在外部对象中创建内部对象:

 OuterClass.InnerClass innerObject = outerObject.new InnerClass();

关于java - 图矩阵项目(静态中的非静态),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898098/

相关文章:

java - 统一成本搜索

javascript - Google Timeline - 根据名称更改数据颜色

php - 谷歌距离矩阵 JSON 最短路径 (PHP)

c++ - 使用密集矩阵和稀疏矩阵

matlab - 在 MATLAB 中构建一个包含向量分量的巨大矩阵

java - 如何修复我的 Arraylist 并将其设置在我的 ListView 中

java - 将转义的转义序列替换为其未转义的值

javascript - 如何在 chart.js 中使用 json 数据

java - 使用 docker-java 从 Amazon ECR 拉取镜像

java - 为什么decodeByteArray 给我一个空指针错误