简体   繁体   English

在通过Java中的另一个函数传递引用时更新字典值

[英]Updating dictionary value by reference when passing it through another function in Java

i am new to Java and was wondering if it is possible to update dictionary's values outside of the dictionary itself. 我是Java的新手,我想知道是否有可能在字典本身之外更新字典的值。 I think i loose a connection to my 'nodesIncluded' set by creating a new instance in the 'process' method in ID2. 我认为我可以通过在ID2的“进程”方法中创建新实例来松开与“ nodesIncluded”集的连接。 Is there any neat way to always keep track of the object underneath the value in a dictionary. 有什么巧妙的方法可以始终跟踪字典中值下方的对象。

Sorry guys, i initially pasted the wrong bit of code :) 抱歉,我最初粘贴了错误的代码:)


public class Dictionary {

    @AllArgsConstructor
    private class Info {
        private Double average;
        private Set nodesIncluded;
    }

    private void initialize(){
        Map<String, Info> testDict = new HashMap<String, Info>(){{
        put("ID001", new Info(1.0d, new HashSet<>(Arrays.asList("a", "b"))));
        put("ID002", null);
        }};

        Info takenOut01 = testDict.get("ID001");
        Info takenOut02 = testDict.get("ID002");

        process(takenOut01, "c");
        process(takenOut02, "c");

        System.out.print(testDict.get("ID001").nodesIncluded.contains("c")); // true
        System.out.print(testDict.get("ID002").nodesIncluded.contains("c")); // NullPointerException
        System.out.print(testDict.get("ID002").nodesIncluded.contains("default")); // NullPointerException
    }

    private void process (Info takenOut, String newValue ) {
        if (takenOut == null) {
            takenOut = new Info(0.0d, new HashSet<>(Arrays.asList("default")));
        }
        takenOut.nodesIncluded.add(newValue);
    }

    public static void main(String[] args) {
        Dictionary test = new Dictionary();
        test.initialize();
    }
}

Java passes by value, but when it comes to objects, it's a little confusing. Java通过值传递,但涉及对象时,会有些混乱。 When objects are passed through a method, its reference value gets passed. 当对象通过方法传递时,其引用值将被传递。 This means that operations performed on the object within the method scope affects it outside of its scope. 这意味着在方法范围内对对象执行的操作会影响其范围之外的对象。

public static class Bar {
    private int num = 0;
    public int getNum() { return num; }
    public void setNum(int num) { this.num = num; }
}

public static void setBar5(Bar bar) {
    bar.setNum(5);
    System.out.println(bar.getNum()); // outputs 5
}

public static void main(String[] args) {
    Bar bar = new Bar();
    setBar5(bar);

    System.out.println(bar.getNum());
}

In short, if you hold an object reference to an object that is also inside a Collection, any operations on it would look like you're modifying it. 简而言之,如果您拥有一个对象引用,该对象引用也位于Collection内部,则对该对象的任何操作都好像您正在对其进行修改。

Bar bar = new Bar();
List<Bar> bars = new ArrayList<>();
bars.add(bar);
bar.setNum(5);

for(Bar b : bars) {
    System.out.println(b.getNum());
}
// outputs 5

If you get the object from the collection and modify it, it will also affect any variables holding onto that object. 如果您从集合中获取对象并对其进行修改,那么它还将影响持有该对象的所有变量。

Bar bar2 = bar;

// modifying the element in the collection
bars.get(0).setNum(4);

// Even when we modify bar, bar2 gets affected.
System.out.println(bar2.getNum()); // outputs 4

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

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