2015-12-05 49 views
0

我在我的項目中使用了一個數組double xyz [20000] [20000],但java給出了「堆空間」錯誤。我打算使用Hashmaps而不是上面的數組。所以我需要使用Hashmap。 現在的問題是,與java數組一樣,如果需要訪問一個元素,我們可以像xyz [10] [10]那樣訪問它,但是如果我使用上面的HashMap,那麼我如何訪問像key = 10這樣的特定元素,相應陣列中的第10個元素?訪問字符串&ArrayList的HasMap的特定元素

回答

0

HashMap總是以這種方式工作。您已經在java的HashMap類中指定了函數來獲取密鑰的值,getKey()getValue()應該爲您提供相應密鑰的密鑰和值。

Map<int, List<Integer>> entry = new HashMap<int, List<Integer>>(); 
//rest of the code, putting values etc. 
//the accessing data of 10th index 
int key = entry.getKey(); 
List<Integer> values = entry.getValue(); 
System.out.println("Key = " + key); 
System.out.println("Values = " + values.get(10) + "\n");