Java hashmaps索引的arraylist超出范围

Java hashmaps索引的arraylist超出范围,java,Java,我希望这不是重复的。我有以下代码: ArrayList<HashMap<BigInteger, HashSet<Integer>>> maps = new ArrayList<HashMap<BigInteger, HashSet<Integer>>>(); maps.get(i).put(key, new HashSet<>()); ArrayList映射=新的ArrayList(); maps.get(i)

我希望这不是重复的。我有以下代码:

ArrayList<HashMap<BigInteger, HashSet<Integer>>> maps = new ArrayList<HashMap<BigInteger, HashSet<Integer>>>();
maps.get(i).put(key, new HashSet<>());
ArrayList映射=新的ArrayList();
maps.get(i).put(key,newhashset());

我得到了索引越界错误。我想这是因为我还没有初始化arraylist。有什么评论吗

2015年1月28日下午5:58后编辑:
事情是这样的。我需要检查第I个hashmap(显然我不知道它是否存在)是否包含某个键。我怎么知道第I个hashmap不存在。

您似乎在空的
数组列表上使用
.get(int)
函数

如果要将数据插入
阵列列表
,则需要使用
.add(E)
功能。如果要插入特定索引,可以使用
.add(int,E)
函数。 在您的情况下,这将是:

maps.add(i, new HashMap<BigInteger, HashSet<Integer>>());
maps.get(i).put(key, new HashSet<>());
添加(i,newhashmap()); maps.get(i).put(key,newhashset());
“我想这是因为我还没有初始化arraylist。”是的-当列表为空时,您希望
get(I)
做什么?您无法从空列表中获取内容。好的。主要的是我需要检查arraylist的第I个条目(map)是否包含某个键,然后决定下一步做什么。@Henny。我明白你的意思了。将空的HashMap添加到我需要的每个索引中是否更简单?@qaz是的,只要确保每当您使用
。get(I)
,您已经使用了
。add(I,new HashMap())