具有多维数组的HashMap或Map的Java使用 [英] Java usage of HashMap or Map with multidimensional arrays
问题描述
我想使用 HashMaps< String,V>
,其中值类型 V
应该是一个二维 int
数组( int [] []
)。
I would like to use HashMaps<String, V>
where the value type V
should be a two dimensional int
array (int[][]
).
Map<String,int[][]> map = new HashMap<>();
int[][] a = new int[][]{ {1, 2} };
map.put("test", a);
System.out.println("Test:" + map.containsKey("test")); // Test: true
System.out.println("key ==== " + map.get("test")); // Key === [[I@19a8942
我想要一个访问唯一的值,即一个二维数组,数组的大小是固定的 array [1] [3]
,这是不变的。键的数量会随着时间而增长,但不会增加数组。例如:
I am trying to have a key that access a unique value i.e. a 2-dimensional array, the size of the array is fixed array[1][3]
, this is constant. Number of keys will grow with time but not the arrays. For example:
Key1 -> [no1, no2, no3]
key2 -> [no4, no5, no6]
- 我想访问数组值,现在我得到一个错误。
- 有没有更好的方法来实现这个?理想情况下,我希望按键进行排序。
我不是Java专家,但在阅读了一些地图后,对我来说最好。
I am not an expert in Java but after some reading about maps I felt this works best for me.
推荐答案
我想访问数组值,现在我得到错误。
I would like to access the array values, now I get an error.
不确定您的意思,但您似乎处于正确的轨道。
Not sure what you mean here, but you seem to be on the right track.
System.out.println(map.get("test")[0][1]);
2
理想情况下,我希望按键排序。
Ideally I would like it to be sorted by keys.
您可以使用 TreeMap
,它按键的自然排序排序。
You can use a TreeMap
, which is sorted on the natural ordering of its keys.
这篇关于具有多维数组的HashMap或Map的Java使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!