简体   繁体   中英

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. So I used for-loop to get data from input.txt file to 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)

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).

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)

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. because it will refer to the same object. chage to NodeArray [i] = new Node();

You need to understand that when you initialize an object, like Node empty = new Node(); this object is created in the heap, and the reference empty points to It.

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. 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.

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)

because, these were the values that you entered to empty object in the last iteration of the loop.

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).

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