如何将二维数组直接添加到哈希图中? [复制]

Posted

技术标签:

【中文标题】如何将二维数组直接添加到哈希图中? [复制]【英文标题】:How to add a two-dimensional array directly to a hashmap? [duplicate] 【发布时间】:2016-08-17 03:35:55 【问题描述】:

而不是这个:

int[][] someArr =   88, 35 ,  11, 98  ;
mapDE = new HashMap<String, int[][]>();
mapDE.put("someKey", someArr);

我想这样做是为了保存一行代码:

mapDE = new HashMap<String, int[][]>();
mapDE.put("someKey",   88, 35 ,  11, 98  );

有什么简单的方法吗?

【问题讨论】:

【参考方案1】:

mapDE.put("someKey", 88, 35 , 11, 98 ); 无法编译,因为 88, 35 , 11, 98 无法确保int[][] 的类型

你可以这样试试:

    Map<String, int[][]> mapDE = new HashMap<String, int[][]>();
    mapDE.put("someKey",new int[][]   88, 35 ,  11, 98  );

【讨论】:

【参考方案2】:

使用这个:

mapDE.put("somekey", new int[][]
   0, 0 ,
   0, 0  );

【讨论】:

【参考方案3】:

您已经很接近了,请尝试以下操作:

mapDE.put("someKey", new int[][]88, 35, 11, 98);

【讨论】:

以上是关于如何将二维数组直接添加到哈希图中? [复制]的主要内容,如果未能解决你的问题,请参考以下文章