Java 如何在int[]的哈希映射中获取特定键的所有值
我有一个下面的搭扣Java 如何在int[]的哈希映射中获取特定键的所有值,java,Java,我有一个下面的搭扣 private static Map<Integer, int[]> hm = new HashMap<Integer, int[]>(); m.put(1, new int[]{1,2,3,4,5}); hm.put(96, new int[]{23,24,2,34,22}); hm.put(101, new int[]{123,11,22,34,5,4,7}); hm.put(96, new int[]{88,87,85,80,9,7,2,1});
private static Map<Integer, int[]> hm = new HashMap<Integer, int[]>();
m.put(1, new int[]{1,2,3,4,5});
hm.put(96, new int[]{23,24,2,34,22});
hm.put(101, new int[]{123,11,22,34,5,4,7});
hm.put(96, new int[]{88,87,85,80,9,7,2,1});
hm.put(96, new int[]{110,123,11,22,34,33,29,4});
然后我还要迭代这些值。数组是可迭代的,因此可以使用for value:array语法对它们进行迭代
Map或HashMap中没有内置任何支持containsValueint[]之类的东西,但这很容易自己实现。数组是可编辑的,因此可以使用for-value:array语法对它们进行迭代 Map或HashMap中没有内置支持containsValueint[]之类的东西,但是您自己可以很容易地实现它。试试这个
if(hm.get(96) != null)
{
for(int i = 0; i < hm.get(96).length ; i++)
System.out.print("Value" + i + "=" + hm.get(96)[i] );
}
试试这个
if(hm.get(96) != null)
{
for(int i = 0; i < hm.get(96).length ; i++)
System.out.print("Value" + i + "=" + hm.get(96)[i] );
}
嘿,谢谢你的帮助,甚至所有其他方法都很有效。这个方法很容易遵循。谢谢你的帮助,甚至所有其他方法都很有效。这个方法很容易遵循。干杯
if(hm.get(96) != null)
{
for(int i = 0; i < hm.get(96).length ; i++)
System.out.print("Value" + i + "=" + hm.get(96)[i] );
}
if (hm.containsKey(route_ID)) {
int[] values = hm.get(route_ID);
for(int i=0; i<values.length; i++)
System.out.println(values[i]);