【发布时间】:2019-08-11 03:41:21
【问题描述】:
假设我有一个Hashmap,我希望两个Integers 的数组实际上是每个值的键。我不知道如何取回正确的值。它应该已经存储在Hashmap
public class Mapky {
public static void main(String[] args) {
HashMap<Integer[], String> mapka = new HashMap<>();
mapka.put(new Integer[]{10,23}, "Hello");
System.out.println(mapka.get(new Integer[]{10,23}));
}
}
还有为什么会这样
System.out.println(new Integer[]{10,23}.equals(new Integer[]{10,23}));
返回false?
【问题讨论】:
-
你不能。数组永远只等于它们自己。使用 hashCode 和 equals() 创建一个适当的类。或者至少,使用 List
。一般避免使用数组。