我使用布尔数组作为HashMap的键.但问题是当一个不同的数组作为键传递时,HashMap无法获取键,尽管元素是相同的.(因为它们是不同的对象).
如何使用数组作为键?这是代码:
public class main {
public static HashMap<boolean[], Integer> h;
public static void main(String[] args){
boolean[] a = {false, false};
h = new HashMap<boolean[], Integer>();
h.put(a, 1);
if(h.containsKey(a)) System.out.println("Found a");
boolean[] t = {false, false};
if(h.containsKey(t)) System.out.println("Found t");
else System.out.println("Couldn't find t");
}
}
Run Code Online (Sandbox Code Playgroud)
无论是阵列a和t包含相同的元素,但HashMap的不返回任何东西t.
我如何使其工作?