[英]Java map with 2D boolean array as values
您好,假設我有一個哈希圖
HashMap<Integer,boolean[][]> map= new HashMap();
如何在2D數組中的地圖上放置真值或假值
HashMap<Integer, boolean[][]> map = new HashMap();
int rowNumber = 1;
boolean[][] array2D = map.get(rowNumber);
array2D[10][20] = true;
//First get the boolean array if it exists.
boolean[][] value = map.get(key);
//If it doesn't exist in the map yet, instantiate it.
if(value == null){
value = new boolean[size1][size2];
map.put(key,value);
}
//Set the boolean value in the array at the correct location
value[index1][index2] = booleanValue;
根據需要填寫鍵,布爾值,大小和索引。
如果我錯了,請糾正我,但似乎您想在地圖中直接輸入布爾值。 設置方式不是由布爾值組成,而是由布爾數組組成。
這就是為什么不能只放置map.put(key, true)
原因map.put(key, true)
因為true
不是boolean[][]
類型,它只是一個普通的布爾型布爾值。 您可能會遇到某種類型的類型不匹配錯誤,因為boolean
s不能只是魔術地變成數組,除非您告訴計算機將布爾值放在數組中的位置。
如果您實際上想制作一個每個桶都包含2D數組的地圖,那么rfeak的答案將解決您的問題。
你的意思是這樣嗎?
int index, row, col = ....
Boolean[][] array = map.get(index);
array[row][col] = true/false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.