如何将键和元素添加到Java中现有的散列table?

How to add key & element to the existing hash table in Java?

这是我的代码:

for (int j = 0; j < modulePass1AL.get(i).modSize; j++) {
    System.out.printf("for module %d: put into entirememoryMapHashtable:%d,%d\n", i, arrayPerModule[j][0], arrayPerModule[j][1]);
    entirememoryMapHashtable.put(arrayPerModule[j][0], arrayPerModule[j][1]);
}

我想将 arrayPerModule[j][0] 和 arrayPerModule[j][1] 添加到名为 entirememoryMapHashtable 的散列 table,这是一个大散列 table这应该保留来自每个 arrayPerModule 的信息(我总共有 4 个 arrayPerModule 数组——它们都有不同的数组长度)。 但是,我认为我的代码是通过不断从 entirememoryMapHashtable 的第 0 个索引添加 arrayPerModule 来更新 entirememoryMapHashtable 的内容。

请帮我解决这个问题。

谢谢!

Map 接口中的

put(...) 方法需要一个键和一个值。请记住,密钥必须是唯一的...

检查 arrayPerModule[j][0] 中是否没有重复元素,否则,您将更新该键的值。如果您不需要使用 arrayPerModule[j][0] 作为键,在这种情况下,您可以使用索引。