简体   繁体   English

使用for循环时发生错误的输出节点数据

[英]wrong output Node data occurs while using for-loop

I want to put a Node in each array, and the Node will contain the data from my input.txt file. 我想在每个数组中放置一个Node,该Node将包含来自input.txt文件的数据。 So I used for-loop to get data from input.txt file to EdgeArray. 因此,我使用了for循环将数据从input.txt文件获取到EdgeArray。

But the result shows like 3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4 (enter) 但是结果显示为3 0 -4(输入)3 0 -4(输入)3 0 -4(输入)3 0 -4(输入)3 0 -4(输入)

My Input file has the data like 4 5 (enter) 0 1 -3 (enter) 1 2 -2 (enter) 0 2 0(enter) 3 2 -1 (enter) 3 0 -4 (enter). 我的输入文件的数据类似4 5(输入)0 1 -3(输入)1 2 -2(输入)0 2 0(输入)3 2 -1(输入)3 0 -4(输入)。

So the result must be like 0 1 -3 (enter) 1 2 -2 (enter) 0 2 0 (enter) 3 2 -1 (enter) 3 0 -4 (enter) 所以结果必须像0 1 -3(enter)1 2 -2(enter)0 2 0(enter)3 2 -1(enter)3 0 -4(enter)

What's wrong and What should I do to fix this? 有什么问题,我应该怎么解决?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Assignment51 {
    public static void main(String[] args) throws IOException {
        BufferedReader in;
        try {
            in = new BufferedReader(new FileReader("input.txt"));

            String graphGuide;
            graphGuide = in.readLine();

            String divider[] = graphGuide.split(" "); //공백을 구분자로 지정하기
            int NodeNum = Integer.parseInt(divider[0]); //Node갯수
            int EdgeNum = Integer.parseInt(divider[1]); //Edge갯수

            Node empty = new Node();

            Node [] NodeArray = new Node [NodeNum];
            for(int i = 0; i < NodeNum; i++){
                NodeArray [i] = empty; 
            }

            Node [] EdgeArray = new Node [EdgeNum];
            for(int i = 0; i < EdgeNum; i++){
                EdgeArray [i] = empty; 
            }

            for(int i = 0; i < EdgeNum; i++){
                String temp;
                temp = in.readLine();

                String divider2[] = temp.split(" ");
                int v1 = Integer.parseInt(divider2[0]);
                int v2 = Integer.parseInt(divider2[1]);
                int weight = Integer.parseInt(divider2[2]);

                EdgeArray[i].VertexA = v1;
                EdgeArray[i].VertexB = v2;
                EdgeArray[i].weight = weight;
            }

            for(int k = 0; k < EdgeNum; k++){
                System.out.println(EdgeArray[k].VertexA + " " + EdgeArray[k].VertexB + " " + EdgeArray[k].weight);
            }

            KruskalsAlgorithm(EdgeArray, EdgeNum);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
            Node empty = new Node();

        Node [] NodeArray = new Node [NodeNum];
        for(int i = 0; i < NodeNum; i++){
            NodeArray [i] = empty; 
        }

        Node [] EdgeArray = new Node [EdgeNum];
        for(int i = 0; i < EdgeNum; i++){
            EdgeArray [i] = empty; 
        }

this place. 这个地方。 you can't initialize like this.NodeArray [i] = empty. 您不能像这样初始化。NodeArray[i] =空。 because it will refer to the same object. 因为它将引用相同的对象。 chage to NodeArray [i] = new Node(); 更改为NodeArray [i] = new Node();

You need to understand that when you initialize an object, like Node empty = new Node(); 您需要了解,在初始化对象时,例如Node empty = new Node(); this object is created in the heap, and the reference empty points to It. 该对象是在堆中创建的,引用empty指向它。

When you make a loop like that: 当您进行这样的循环时:

Node [] EdgeArray = new Node [EdgeNum];
    for(int i = 0; i < EdgeNum; i++){
        EdgeArray [i] = empty; 
    }

It means that you initialize each element in the array with that same object that empty refers to. 这意味着您使用empty引用所指向的同一对象初始化数组中的每个元素。 So, later, when you write something like this: 因此,稍后,当您编写如下内容时:

EdgeArray[i].VertexA = v1;
EdgeArray[i].VertexB = v2;
EdgeArray[i].weight = weight;

What actually happens is, that EdgeArray[i].VertexA is referencing to the empty object, that is stored in the heap, so It is equivalent to empty.VertexA , empty.VertexB and so on. 实际发生的是, EdgeArray[i].VertexA引用了存储在堆中的empty对象,因此等效于empty.VertexAempty.VertexB等。

Each iteration of the loop you change the same object, and that's why you get the output 循环的每次迭代都更改相同的对象,这就是为什么要获得输出的原因

3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4 (enter) 3 0 -4(输入)3 0 -4(输入)3 0 -4(输入)3 0 -4(输入)3 0 -4(输入)

because, these were the values that you entered to empty object in the last iteration of the loop. 因为这些是您在循环的最后一次迭代中输入到empty对象的值。

I hope this was helpful and that now you know what to do to fix your problem (create new object Node() for each element in the array). 我希望这会有所帮助,并且现在您知道该如何解决问题了(为数组中的每个元素创建新的对象Node() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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