需要帮助理解Java中变量的作用域吗

需要帮助理解Java中变量的作用域吗,java,arrays,class,loops,scope,Java,Arrays,Class,Loops,Scope,我正在为学校做一个小程序,它读取一个数据文件,并在控制台中输出结果。我很难弄清楚为什么我的对象数组在我完成一个while循环后不能继续。代码如下: import java.io.*; import java.util.*; public class order{ private static Node nodes[]; public static void main(String[] args) throws FileNotFoundException { S

我正在为学校做一个小程序,它读取一个数据文件,并在控制台中输出结果。我很难弄清楚为什么我的对象数组在我完成一个while循环后不能继续。代码如下:

import java.io.*;
import java.util.*;

public class order{
    private static Node nodes[];

    public static void main(String[] args) throws FileNotFoundException {
        String dataFile = "file.dat";
        indegreeCompute(dataFile);
    }

    private static void indegreeCompute(String dataFile) throws FileNotFoundException {
        Scanner scanFile = new Scanner(new File(dataFile));
        nodes = new Node[scanFile.nextInt()];
        while (scanFile.hasNextLine()) {
            scanFile.nextLine();
            int index = scanFile.nextInt();
            System.out.print("node = " + index);
            scanFile.next();
            int NumOutDegree = scanFile.nextInt();
            nodes[index].outDegree = NumOutDegree;
            System.out.print(" outDegree = " + nodes[index].outDegree);
            nodes[index].adjNodes = new int[NumOutDegree];
            scanFile.next();
            System.out.print(" adjNodes = [");
            for ( int i = 0; i < nodes[index].outDegree; i ++ ) {
                nodes[index].adjNodes[i] = scanFile.nextInt();
                if (i < nodes[index].outDegree - 1)
                    System.out.print(nodes[index].adjNodes[i] + ", ");
                else
                    System.out.print(nodes[index].adjNodes[i]);
            }
            System.out.print("]");
            System.out.println();
        }

        for (int i = 0; i < nodes.length; i ++) {
            int outDegree = nodes[i].outDegree;
            for(int k = 0; k < outDegree; k ++) {
                int adjNode = nodes[i].adjNodes[k];
                nodes[adjNode].inDegree ++;
            }
        }
        scanFile.close();
    }
}
任何帮助或解释,以帮助我理解将不胜感激。谢谢

以下是该文件的内容:

14 //numNodes; space after '(' and before ')' to simplify reading the file
 0 (  1 ): 10 //node ( outdegree ): adjNodes in arbitrary order
 1 ( 10 ):  0  5  8 10  6  9  7 11 12 13
 2 (  1 ):  7
 3 (  3 ):  1  10 11
 4 (  3 ):  0  1  5
 5 (  3 ):  6  7  10
 6 (  2 ):  7 10
 7 (  5 ):  8 10  12 11 13
 8 (  1 ): 13
 9 (  1 ):  0
10 (  0 ):
11 (  0 ):
12 (  2 ): 10 11
13 (  1 ): 12

在为节点[index]的属性指定任何值之前,应先设置节点[index]=new Node()。节点类的属性不应该是静态的,因为每个节点都应该有自己的值

因为节点数组中的每个元素都将为null,除非为其设置值。在代码中,当您将值设置为nodes[index].outDegree时,实际上是在设置Node.outDegree的值,而nodes[index]仍然为空。最后,你会得到一个满是null的数组


总之,这里有两个错误。错误地使用静态关键字,并且在使用对象数组项之前忘记初始化它

以下内容应该适合您

    public class Node {
     //public static  int inDegree, outDegree, adjNodes[]; // static was causing the problem
     public int inDegree, outDegree, adjNodes[];
    }


public class order{
    private static Node nodes[];

    public static void main(String[] args) throws FileNotFoundException {
        String dataFile = "file.dat";
        indegreeCompute(dataFile);
    }

    private static void indegreeCompute(String dataFile) throws FileNotFoundException {
        Scanner scanFile = new Scanner(new File(dataFile));
        nodes = new Node[scanFile.nextInt()];
        for(int l= 0 ; l <nodes.length ; l++) {
            nodes[l] = new Node();
        }
        while (scanFile.hasNextLine()) {
            scanFile.nextLine();
            int index = scanFile.nextInt();
            System.out.print("node = " + index);
            scanFile.next();
            int NumOutDegree = scanFile.nextInt();
            nodes[index].outDegree = NumOutDegree;
            System.out.print(" outDegree = " + nodes[index].outDegree);
            nodes[index].adjNodes = new int[NumOutDegree];
            scanFile.next();
            System.out.print(" adjNodes = [");
            for ( int i = 0; i < nodes[index].outDegree; i ++ ) {
                nodes[index].adjNodes[i] = scanFile.nextInt();
                if (i < nodes[index].outDegree - 1)
                    System.out.print(nodes[index].adjNodes[i] + ", ");
                else
                    System.out.print(nodes[index].adjNodes[i]);
            }
            System.out.print("]");
            System.out.println();
        }

        for (int i = 0; i < nodes.length; i ++) {
            int outDegree = nodes[i].outDegree;
            for(int k = 0; k < outDegree; k ++) {
                int adjNode = nodes[i].adjNodes[k];
                nodes[adjNode].inDegree ++;
            }
        }
        scanFile.close();
    }
}
公共类节点{
//public static int inDegree、outDegree、adjNodes[];//问题是由static引起的
公共int-inDegree、outDegree、adjNodes[];
}
公共阶级秩序{
私有静态节点[];
公共静态void main(字符串[]args)引发FileNotFoundException{
字符串dataFile=“file.dat”;
indegreeCompute(数据文件);
}
私有静态void indegreeCompute(字符串数据文件)引发FileNotFoundException{
扫描仪扫描文件=新扫描仪(新文件(数据文件));
节点=新节点[scanFile.nextInt()];

对于(int l=0;l)你能发布topoOrder.dat的内容吗?你程序的输出(第一个for循环)与你期望的匹配吗?是的,第一个循环中的输出是完美的。我只是在第二个循环中遇到了问题。
    public class Node {
     //public static  int inDegree, outDegree, adjNodes[]; // static was causing the problem
     public int inDegree, outDegree, adjNodes[];
    }


public class order{
    private static Node nodes[];

    public static void main(String[] args) throws FileNotFoundException {
        String dataFile = "file.dat";
        indegreeCompute(dataFile);
    }

    private static void indegreeCompute(String dataFile) throws FileNotFoundException {
        Scanner scanFile = new Scanner(new File(dataFile));
        nodes = new Node[scanFile.nextInt()];
        for(int l= 0 ; l <nodes.length ; l++) {
            nodes[l] = new Node();
        }
        while (scanFile.hasNextLine()) {
            scanFile.nextLine();
            int index = scanFile.nextInt();
            System.out.print("node = " + index);
            scanFile.next();
            int NumOutDegree = scanFile.nextInt();
            nodes[index].outDegree = NumOutDegree;
            System.out.print(" outDegree = " + nodes[index].outDegree);
            nodes[index].adjNodes = new int[NumOutDegree];
            scanFile.next();
            System.out.print(" adjNodes = [");
            for ( int i = 0; i < nodes[index].outDegree; i ++ ) {
                nodes[index].adjNodes[i] = scanFile.nextInt();
                if (i < nodes[index].outDegree - 1)
                    System.out.print(nodes[index].adjNodes[i] + ", ");
                else
                    System.out.print(nodes[index].adjNodes[i]);
            }
            System.out.print("]");
            System.out.println();
        }

        for (int i = 0; i < nodes.length; i ++) {
            int outDegree = nodes[i].outDegree;
            for(int k = 0; k < outDegree; k ++) {
                int adjNode = nodes[i].adjNodes[k];
                nodes[adjNode].inDegree ++;
            }
        }
        scanFile.close();
    }
}