如何填写清单清单? [英] How to fill a List of Lists?

查看:70
本文介绍了如何填写清单清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个这样的列表列表:

I create a list of lists like this:

List<List> tmp = new ArrayList<List>(2);

然后我要在第一个子列表中插入10,如下所示:

Then I'd like to insert 10 to first sub-list as follows:

tmp.get(0).add(10);

但是,出现以下错误:

线程主"中的异常java.lang.IndexOutOfBoundsException:索引:0,大小:0在java.util.ArrayList.rangeCheck(ArrayList.java:653)在java.util.ArrayList.get(ArrayList.java:429)

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429)

错误的根源是什么,我该如何克服?

What is the source of error and how can I overcome it ?

推荐答案

您已经创建了一个初始容量为 2 的空列表(即,列表的内部表示将不会重新调整大小,直到您已经向其中添加了2个元素,并且正在添加第三个元素.

You've created an empty list with initial capacity of 2 (i.e. the internal representation of the list won't be resized until you've added 2 elements to it and are adding the third).

然后,您尝试从空列表中获取第一个元素.自然这是行不通的.您需要首先
add()尽可能多的内部列表(大概2个),然后然后填充这些内部列表.

Then you try to get the first element from the empty list. Naturally this won't work. You need to first add() as many inner lists (presumably 2) as you want, and then fill those inner lists.

这篇关于如何填写清单清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆