如何访问 TreeMap 中的特定元素?

发布于 2024-12-11 03:26:15 字数 443 浏览 0 评论 0原文

我使用树形图将数据添加到我的系统中。 我希望用户能够更改已添加到系统的对象中的某些值。 为了让用户选择他/她想要更改的值,我已使用它将其打印到屏幕上。

for(Employee employee : employeeMap.values())
{
    System.out.println("Employe no :" +x+": "+employee);
    x++;
}

然后,为了访问用户选择的树图中的对象元素,我尝试使用以下方法:

 System.out.println("this employee @@@@@##### is no: "+employeeMap.get(x));

这就是我在数组列表中成功使用的内容,但它似乎不起作用,使用时似乎所有值都为空这种方法。

有人可以告诉我一种更好的方法吗?

I have used a Tree map to add data into my system.
I would like the user to be able to change certain value within the objects that have been added to the system.
In order for the user to choose the value that he/she wants to change I have printed it out to the screen using this.

for(Employee employee : employeeMap.values())
{
    System.out.println("Employe no :" +x+": "+employee);
    x++;
}

Then in order to access the objects element in the tree map that the user has chosen I have tried using this:

 System.out.println("this employee @@@@@##### is no: "+employeeMap.get(x));

Which is what I have successfully used with arraylists, But it does not seem to work it seems that all the value are null when using this approach.

Could some one please show me a better way of doing it.

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

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

发布评论

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

评论(3

少女七分熟 2024-12-18 03:26:15

访问 Map 中的条目时,必须使用键,而不是像 ArrayList 中那样使用索引。如果该键不存在,您将返回 null,这就是您所看到的。在您的示例中,您已经可以访问 for 循环中的 employee 对象,因此您不需要 get 它再次从地图上。

您可能想考虑使用 employeeMap.entrySet() 来一次迭代键和值,然后您可以说 employeeEntry.getKey()查看给定条目的密钥是什么,如下所示(省略泛型,因为我不知道您的密钥类型,并且还假设密钥是员工编号):

for(Map.EntrySet employeeEntry : employeeMap.entrySet()) {
    System.out.println("Employee no :" + employeeEntry.getKey() +": " + employeeEntry.getValue());
}

When accessing entries in a Map, you must use the key, not the index as in ArrayLists. If that key does not exist, you will get back null, which is what you are seeing. In the case of your example, you already have access to the employee object in your for loop, so you don't need to get it again from the map.

You might want to look at using employeeMap.entrySet() instead to iterate over both the key and value at once, and then you can say something like employeeEntry.getKey() to see what the key is for a given entry, like this (leaving out the generics because I don't know your key's type, and also assuming the key is the employee number):

for(Map.EntrySet employeeEntry : employeeMap.entrySet()) {
    System.out.println("Employee no :" + employeeEntry.getKey() +": " + employeeEntry.getValue());
}
眼前雾蒙蒙 2024-12-18 03:26:15

Map 是键值对。您只是无法使用任何索引访问 Map 中的值(就像您使用 x 并在循环 x++ 中递增它一样。)您需要使用您在插入数据时输入的键。我建议你采用以下方式。

for(String key:employeeMap.keySet())
{
    System.out.println("Employe no :" +x+": "+employee.get(key));
    x++;
}

Map is key value pair.you just cannot access values from map using any index(like you are using x and incrementing it in a loop x++.) you need to use that key u entered while insertying data. i would suggest you the following way.

for(String key:employeeMap.keySet())
{
    System.out.println("Employe no :" +x+": "+employee.get(key));
    x++;
}
揪着可爱 2024-12-18 03:26:15

基本上,您可以通过键访问地图值

在您的代码片段中,employeeMap.get(x),“x”看起来不像键。向我们展示您如何填充employeeMap

已更新

由于您使用员工 ID 作为键,因此您可以使用它来检索值

for (Employee employee : employeeMap.values()) {
    System.out.println("Employe no :" +employee.getRandomIDno()+": "+employee);
    Object thekey = employee.getRandomIDno();
}

,然后,

System.out.println("this employee @@@@@##### is no: "+employeeMap.get(thekey));    

Basically you access your map values by key

In your snippet, employeeMap.get(x), 'x' doesnt look like the key. Show us how you populate employeeMap

Updated

Since you are using the employee id as a key you can use it to retrieve the value

for (Employee employee : employeeMap.values()) {
    System.out.println("Employe no :" +employee.getRandomIDno()+": "+employee);
    Object thekey = employee.getRandomIDno();
}

and later,

System.out.println("this employee @@@@@##### is no: "+employeeMap.get(thekey));    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文