gpt4 book ai didi

java - 获取 LinkedList 中 map 对象的索引

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:15 25 4
gpt4 key购买 nike

您能帮我检索添加到 LinkedList 的 map 对象的索引吗?

int p;
List<Map> list=new LinkedList<Map>();
Map<Integer, Integer> map=new LinkedHashMap<Integer,Integer>();
for(int i=2;i<=n;i=i+2){
if(i==n && i%2!=0){
map.put(i, i+1);
list.add(map);
}else{
map.put(i,0);
list.add(map);
}
}

如何检索带有键“p”的 map 对象的索引

我尝试了下面的方法,但我对方法的理解可能是错误的。请帮忙

list.indexOf(map.get(p));

最佳答案

您可以遍历列表并检查哪个 map 具有此键:

int p = /* key to find */
int index = -1;
for (int i = 0; i < list.size(); ++i) {
if (list.get(i).containsKey(p)) {
index = i;
break;
}
}

关于java - 获取 LinkedList 中 map 对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53019547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com