关于传递 HashMap 引用的基本问题

发布于 2024-09-28 19:09:25 字数 741 浏览 0 评论 0原文

我已经定义了一个 HashMap,它使用 double 类型键和另一个 HashMap 作为值,如图所示。

HashMap<Double, HashMap<Double, String>> HM1 = new HashMap<Double, HashMap<Double, String>>();

现在,对于此 HashMap 的每个条目,我都有一个对不同 HashMap 的引用;其名称源自此 HashMap 中该条目的键值。例如: 如果我HM1中的键值为8,那么“Alpha8”中要引用的HashMap的名称。如果HM1中的键值是6,那么要引用“Alpha6”中的HashMap的名称。所以我将这些添加到 HashMap HM1 的语法是 HM1.put(8, Alpha8);和 HM1.put(6,Alpha6);

我的问题:

键值是预先定义的,我正在从文本文件中读取这些值。因此,我打开文件,编写一个扫描器对象来选取每个值并将其放入双精度类型变量keyvalue中。但是,为了获取该键的值,我定义了一个字符串 s1 = "Alpha"+keyvalue.toString();

我的主要问题是如何在 put 函数中传递这个字符串。因为如果我说 HM1.put(keyvalue, s1);它相当于传递一个双键和一个字符串值,而不是传递一个双键和对另一个 HashMap 的引用。对于原始数据类型,您也许可以包装,但对于 HashMap 引用,我不知道该怎么做。

I have defined a HashMap which uses a double type key and another HashMap as value as shown

HashMap<Double, HashMap<Double, String>> HM1 = new HashMap<Double, HashMap<Double, String>>();

Now for each entry of this HashMap I have a reference to a different HashMap; the name of which is derived from the key value of that entry in this HashMap. For example:
If my key value in HM1 is 8, then the name of the HashMap to be referenced in "Alpha8". If the key value in HM1 is 6, then the name of the HashMap to be references in "Alpha6". So my syntax in adding these to the HashMap HM1 is HM1.put(8, Alpha8); and HM1.put(6,Alpha6);

My problem:

The key values are pre-defined which I am reading from a text file. Hence, I open the file, write a scanner object to pick each value and put it in a double type variable keyvalue. However, to get the value for this key, I defined a string s1 = "Alpha"+keyvalue.toString();

My main problem is how do I pass this string in my put function. Because if I say HM1.put(keyvalue, s1); it is the equivalent of passing a double key and a string value rather than a double key and the reference to another HashMap. For primitive data types, you may be able to wrap but for a HashMap reference, I'm not sure how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-10-05 19:09:25

假设您拥有所有可能的 Alpha* 引用...我只会将该逻辑放在一个简单的 if 条件中,然后这样做

if(keyvalue == 1) 
{
HM1.put(keyvalue, Alpha1)
} 
else(keyvalue == 2)
{

}
--

Assuming you have all the possible Alpha* references available to you... I will just put that logic in a simple if condition and do like this

if(keyvalue == 1) 
{
HM1.put(keyvalue, Alpha1)
} 
else(keyvalue == 2)
{

}
--
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文