简体   繁体   English

将变量作为参数传递给哈希图的问题

[英]Issue with passing variables as parameters to a hashmap

I'm having an issue with trying to pass a double constant as a parameter into a hashmap. 我在尝试将双精度常量作为参数传递到哈希图中时遇到问题。

public static final double Apple_Initial_Value = 30.0;
private static HashMap<String, Double> base_values = new HashMap<String, Double>() {{

    put("Apple", Apple_Initial_Value);
}};
System.out.println(base_values.get("Apple"));

Results in the following output: 结果如下:

0.0

However, 然而,

public static final double Apple_Initial_Value = 30.0;
private static HashMap<String, Double> base_values = new HashMap<String, Double>() {{

    put("Apple", 30.0);
}};
System.out.println(base_values.get("Apple"));

Results in the following output: 结果如下:

30.0

Having just finished a course in C, my mind jumped to it being some sort of pass by reference vs. pass by value issue, but as I understand it, java is only pass by value, so I'm at a loss. 刚完成C语言课程后,我的脑海跳到了某种形式的按引用传递还是按值传递问题,但是据我了解,java只是按值传递,所以我很茫然。

Cheers, and thanks in advance! 干杯,并预先感谢!

Edit: My apologies, it looks like the code I posted above is not actually indicative of what is exactly happening in my code (as it's actually working!). 编辑:抱歉,我上面发布的代码似乎实际上并不指示代码中到底发生了什么(因为它实际上在工作!)。 To be more specific, the situation is broken into two classes, IR.java, and Main.java. 更具体地说,情况分为两类,IR.java和Main.java。 The relevant parts of IR.java look like this: IR.java的相关部分如下所示:

public class IR {

    public static final double Apple_Initial_Value = 30;
    public static HashMap<String, Double> base_values = new HashMap<String, Double>() {{

        put("Apple", Apple_Initial_Value);

    }};

    public static double get(String item) {
        double value = (double) base_values.get(item);
        return value;
    }
}

And the relevant parts of Main.java look like this: Main.java的相关部分如下所示:

public class Main {

    public static void main(String[] args) {

        System.out.println(IR.get("Apple"));
    }
}

The code above after the edit is completely valid, as I accidentally made it work by adding a typo that was not originally present in my source code when I posted the question (figure that). 修改后,上面的代码是完全有效的,因为我在发布问题时偶然添加了原来不在我的源代码中的错字(图)使它起作用。 My source code declared Apple_Initial_Value as such: 我的源代码这样声明Apple_Initial_Value:

public static Apple_Initial_Value = 50;

but the working code instead declares it as: 但是工作代码将其声明为:

public static final Apple_Initial_Value = 50;

Sorry for leading you all on a wild goose chase, and thanks immensely for your time! 很抱歉带领大家一起追赶鹅,非常感谢您的宝贵时间!

To make an example of myself, I advise any new posters to please read https://stackoverflow.com/help/mcve to learn how to make Minimal, Complete and Verifiable examples before posting questions that contain code. 为了给自己做一个榜样,我建议任何新的发布者在发布包含代码的问题之前,请阅读https://stackoverflow.com/help/mcve,以了解如何制作最小,完整和可验证的示例。

You can try like this: 您可以这样尝试:

public static void main(String[] args) {
    final double Apple_Initial_Value = 30.0;
    HashMap<String, Double> base_values = new HashMap<String, Double>() {{

        put("Apple", Apple_Initial_Value);
    }};
    System.out.println(base_values.get("Apple"));

}

Update for your new code: The variable name is incorect. 更新新代码:变量名称不正确。 Just simple change it in the put statement: 只需在put语句中更改它即可:

put("Apple", Apple_Pie_Initial_Value);

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

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