本文介紹了HashMap<int[],string>通過考慮它們的值來映射整數數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
Given the following code, I get a null
(what I want is "1234"). But I wish to have a map that could consider the key as equals, if the contents of int[]
are equals (rather than consider that the references of int[]
), how should I do it?
HashMap<int[], String> maps=new HashMap<int[], String>();
int[] i=new int[]{1,2,3};
int[] j=new int[]{1,2,3};
maps.put(i,"1234");
System.out.print(maps.get(j));
I am opened to any map that allows keeping int[]
as the key (including TreeMap
) and so on, with the side condition that if that does not hamper on the effectiveness of the map accessing time.
解決方案
No way to do that with arrays, because they don't override equals() and hashCode(). You should define your own class wrapping the array, which would override equals()
and hashCode()
or use a List<Integer>
as key instead.
這篇關于HashMap<int[],string>通過考慮它們的值來映射整數數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!