Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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 这两种情况都是“习惯”;节点";类在向每个实例传递唯一值时打印相同的值_Java_Static_Tree - Fatal编程技术网

Java 这两种情况都是“习惯”;节点";类在向每个实例传递唯一值时打印相同的值

Java 这两种情况都是“习惯”;节点";类在向每个实例传递唯一值时打印相同的值,java,static,tree,Java,Static,Tree,我正在尝试基于条目对象创建一个新节点。我检索了两个不同的条目并打印出它们的键和值。打印出来的两把钥匙都不一样。我将每个键和值存储到单独的“节点”、n1和n2中。Node是我创建的一个自定义类,下面可以找到它的代码。当我打印出n1的键和n2的键的值时,它们应该不同,因为条目的键是,但出于某种原因,它打印出相同的值。我不知道为什么会发生这种情况,我也不明白。这是我目前拥有的代码: 节点类: import java.util.HashMap; import java.util.Map.Entry;

我正在尝试基于条目对象创建一个新节点。我检索了两个不同的条目并打印出它们的键和值。打印出来的两把钥匙都不一样。我将每个键和值存储到单独的“节点”、n1和n2中。Node是我创建的一个自定义类,下面可以找到它的代码。当我打印出n1的键和n2的键的值时,它们应该不同,因为条目的键是,但出于某种原因,它打印出相同的值。我不知道为什么会发生这种情况,我也不明白。这是我目前拥有的代码:

节点类:

import java.util.HashMap;
import java.util.Map.Entry;

public class Node {
    public static Entry<String, Integer> keyVal;
    public static Node leftChild;
    public static Node rightChild;
    public static String bitCode;

    public Node() {
        keyVal = null;
        leftChild = null;
        rightChild = null;
    }

    public Node(Entry<String, Integer> entry) {
        keyVal = entry;
        leftChild = null;
        rightChild = null;
    }

    public Node(Entry<String, Integer> entry, Node n1, Node n2) {
        keyVal = entry;
        leftChild = n1;
        rightChild = n2;
    }

    public Entry<String, Integer> getEntry() { return keyVal; }

    public Node getLeftChild() { return leftChild; }

    public Node getRightChild() { return rightChild; }

    public void setLeftChild(Node lc) {
        leftChild = lc;
    }

    public void setRightChild(Node rc) {
        leftChild = rc;
    }
}
private static Node createTree(PriorityQueue<Entry<String, Integer>> pq) {
    map = new HashMap<>();    // Defined as a public attribute to the Main class
    Entry<String, Integer> entry1 = null;
    Entry<String, Integer> entry2 = null;
    Entry<String, Integer> parent = null;
    Node n1 = null;
    Node n2 = null;
    Node n3 = null;
    System.out.println("PQ size: " + pq.size());

    while (pq.size() > 1) {
        entry1 = pq.poll();
        // Prints "Entry1: R 1"
        System.out.println("Entry1: " + entry1.getKey() + " " + entry1.getValue());    

        entry2 = pq.poll();
        // Prints "Entry2: e 1"
        System.out.println("Entry2: " + entry2.getKey() + " " + entry2.getValue());

        // Never enters if, goes to else
        if (map.containsKey(entry1.getKey())) {
            System.out.println("Map contains entry1!");
            n1 = map.get(entry1.getKey());
        } else {
            n1 = new Node(entry1);              
        }
        // Never enters if, goes to else
        if (map.containsKey(entry2.getKey())) {
            System.out.println("Map contains entry2!");    
            n2 = map.get(entry2.getKey());
        } else {
            n2 = new Node(entry2);              
        }
        // Should print "N1: R 1", Instead prints "N1: e 1"
        System.out.println("N1: " + n1.getEntry().getKey() + " " + n1.getEntry().getValue());
        // Prints "N2: R 1"
        System.out.println("N2: " + n2.getEntry().getKey() + " " + n2.getEntry().getValue());
    }
import java.util.HashMap;
导入java.util.Map.Entry;
公共类节点{
公共静态输入keyVal;
公共静态节点leftChild;
公共静态节点rightChild;
公共静态字符串位码;
公共节点(){
keyVal=null;
leftChild=null;
rightChild=null;
}
公共节点(条目){
keyVal=输入;
leftChild=null;
rightChild=null;
}
公共节点(条目、节点n1、节点n2){
keyVal=输入;
leftChild=n1;
rightChild=n2;
}
公共条目getEntry(){return keyVal;}
公共节点getLeftChild(){return leftChild;}
公共节点getRightChild(){return rightChild;}
公共void setLeftChild(节点lc){
leftChild=lc;
}
公共void setRightChild(节点rc){
leftChild=rc;
}
}
主类中的createTree方法:

import java.util.HashMap;
import java.util.Map.Entry;

public class Node {
    public static Entry<String, Integer> keyVal;
    public static Node leftChild;
    public static Node rightChild;
    public static String bitCode;

    public Node() {
        keyVal = null;
        leftChild = null;
        rightChild = null;
    }

    public Node(Entry<String, Integer> entry) {
        keyVal = entry;
        leftChild = null;
        rightChild = null;
    }

    public Node(Entry<String, Integer> entry, Node n1, Node n2) {
        keyVal = entry;
        leftChild = n1;
        rightChild = n2;
    }

    public Entry<String, Integer> getEntry() { return keyVal; }

    public Node getLeftChild() { return leftChild; }

    public Node getRightChild() { return rightChild; }

    public void setLeftChild(Node lc) {
        leftChild = lc;
    }

    public void setRightChild(Node rc) {
        leftChild = rc;
    }
}
private static Node createTree(PriorityQueue<Entry<String, Integer>> pq) {
    map = new HashMap<>();    // Defined as a public attribute to the Main class
    Entry<String, Integer> entry1 = null;
    Entry<String, Integer> entry2 = null;
    Entry<String, Integer> parent = null;
    Node n1 = null;
    Node n2 = null;
    Node n3 = null;
    System.out.println("PQ size: " + pq.size());

    while (pq.size() > 1) {
        entry1 = pq.poll();
        // Prints "Entry1: R 1"
        System.out.println("Entry1: " + entry1.getKey() + " " + entry1.getValue());    

        entry2 = pq.poll();
        // Prints "Entry2: e 1"
        System.out.println("Entry2: " + entry2.getKey() + " " + entry2.getValue());

        // Never enters if, goes to else
        if (map.containsKey(entry1.getKey())) {
            System.out.println("Map contains entry1!");
            n1 = map.get(entry1.getKey());
        } else {
            n1 = new Node(entry1);              
        }
        // Never enters if, goes to else
        if (map.containsKey(entry2.getKey())) {
            System.out.println("Map contains entry2!");    
            n2 = map.get(entry2.getKey());
        } else {
            n2 = new Node(entry2);              
        }
        // Should print "N1: R 1", Instead prints "N1: e 1"
        System.out.println("N1: " + n1.getEntry().getKey() + " " + n1.getEntry().getValue());
        // Prints "N2: R 1"
        System.out.println("N2: " + n2.getEntry().getKey() + " " + n2.getEntry().getValue());
    }
私有静态节点createTree(PriorityQueue pq){
map=new HashMap();//定义为主类的公共属性
条目entry1=null;
条目entry2=null;
输入父项=null;
节点n1=null;
节点n2=null;
节点n3=null;
System.out.println(“PQ大小:+PQ.size());
而(pq.size()>1){
entry1=pq.poll();
//打印“Entry1:R 1”
System.out.println(“Entry1:+Entry1.getKey()+”+Entry1.getValue());
entry2=pq.poll();
//打印“Entry2:e1”
System.out.println(“Entry2:+Entry2.getKey()+”+Entry2.getValue());
//如果去了别的地方,就不要进去
if(map.containsKey(entry1.getKey()){
System.out.println(“Map包含entry1!”);
n1=map.get(entry1.getKey());
}否则{
n1=新节点(入口1);
}
//如果去了别的地方,就不要进去
if(map.containsKey(entry2.getKey()){
System.out.println(“Map包含entry2!”);
n2=map.get(entry2.getKey());
}否则{
n2=新节点(入口2);
}
//应该打印“N1:R1”,而不是打印“N1:E1”
System.out.println(“N1:+N1.getEntry().getKey()+”“+N1.getEntry().getValue());
//打印“N2:R1”
System.out.println(“N2:+N2.getEntry().getKey()+”“+N2.getEntry().getValue());
}

发生这种情况是因为类
节点中的成员是静态的:

//From the 'Node' class:
public static Entry<String, Integer> keyVal;
public static Node leftChild;
public static Node rightChild;
public static String bitCode;
//...
您可以在以下位置替代第一处输入的值:

n2 = new Node(entry2);

这是因为类
节点中的成员是静态的:

//From the 'Node' class:
public static Entry<String, Integer> keyVal;
public static Node leftChild;
public static Node rightChild;
public static String bitCode;
//...
您可以在以下位置替代第一处输入的值:

n2 = new Node(entry2);

@迈克尔:不客气。如果我帮了忙,我希望你能把我的答案标记为已被接受。谢谢!刚刚做了!我不得不等5分钟左右才允许我回答accept@Michael不客气。如果我帮了忙,如果你能把我的答案标记为已接受,我会很高兴的。谢谢!刚刚做了!我不得不等5分钟左右,它才允许我接受