使用.ADD(index,element)时,在arrayList中获取indexoutofBoundSexception

发布于 2025-02-12 23:20:55 字数 275 浏览 2 评论 0原文

我正在使用list.Add(index,element)函数将元素插入draylist,其中索引不符合订单。

对于 首先,我调用list.add(5,element5) 然后list.Add(3,Element3)

我获得了异常java.lang.indexoutofboundsexception:无效索引5,大小为0 exception。

请告诉我在哪里做错了,我该如何解决。

I am using the list.add(index, element) function to insert elements into an ArrayList, where the index is not in order.

For eg,
first i call list.add(5, element5)
and then list.add(3, element3)

I am getting the exception java.lang.IndexOutOfBoundsException: Invalid index 5, size is 0 exception.

Please tell where I am doing wrong and how can I fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城缀染半城烟沙 2025-02-19 23:20:55

您不能将元素添加到尚不存在的索引中。通常,正如Japhei所说,您只能将元素添加到索引较小或等于阵列长度。这意味着,如果您的ArrayList仍然为空,则只能在索引0上添加元素,或者不指定索引(只需将其添加到末端)。

您想做的是用空元素初始化arraylist。通常,我使用毫无意义的值(例如0或-1)作为整数或空字符串,具体取决于数组类型(或无效元素),然后稍后将它们填充。

但是,如果您知道自己有多少个元素,或者需要什么数尺寸,为什么不使用普通数组呢?那将是正确的方法。

You cannot add elements to indexes which do not yet exist. In general, as Japhei said, you can only add elements to indexes smaller or equal to the array length. This means, if your ArrayList is still empty, you can only add elements at index 0 or without specifying the index (which will just add it to the end).

What you want to do is initialize your ArrayList with empty elements. I normally use meaningless values like 0 or -1 for integers or empty strings depending on the array type (or null elements), and just fill them later.

But if you know how many elements you have, or what array size you need, why not just use a normal array? That would be the right way to do it.

娇纵 2025-02-19 23:20:55

问题是您的arrayList是空的,因此插入(通过add(int index,e element)失败。

请考虑使用add(e element)< /code>(文档)将元素添加到列表的末尾。

The problem is that your ArrayList is empty and therefore the insert (via add(int index, E element) fails.

Consider using the add(E element) (documentation) to add the element to the end of the list.

心如狂蝶 2025-02-19 23:20:55

您只能使用现有的索引或比最后一个现有的索引。否则,您将有一些斑点,其中没有元素。
如果您需要ficxed长度将元素存储在指定位置上,请尝试在空文字之前填写列表或使用数组:

MyElement[] myArray = new MyElement[theLengthOfMyArray];
myArray[5] = elementXY;

或填写带有空元素的列表():

List<MyElement> myList = new ArrayList<>();
for (int i = 0; i < theTargetLengthOfMyList; i++) {
    myList.add(null);
}
myList.set(5, elementXY);

You can only use indexes that are existing or one larger than the last existing. Otherwise you would have some spots with no element in it.
If you need a ficxed length to store elements on a specified position, try to fill the List before with empty entries or use an array:

MyElement[] myArray = new MyElement[theLengthOfMyArray];
myArray[5] = elementXY;

Or fill a List with null elements (shown here):

List<MyElement> myList = new ArrayList<>();
for (int i = 0; i < theTargetLengthOfMyList; i++) {
    myList.add(null);
}
myList.set(5, elementXY);
水晶透心 2025-02-19 23:20:55

因此,可能发生的事情可能是,您在创建上述列表上为列表定义了尺寸。根据您的错误消息,这将是0。在大小0的列表上,您无法将第5或第3位设置为任何事物,因为它们不存在。如果您添加定义变量“列表”的行,我们可以进一步帮助您!

So what is probably happening is, that you defined a size for your list on creating said list. According to your error message this would be 0. And on a list with the size 0, you can't set the 5th or 3rd position to anything, as they don't exist. If you would add the line where you define the variable "list", we could help you further!

滴情不沾 2025-02-19 23:20:55

您不能在列表范围内添加到索引:

indexoutofboundsexception-如果索引超出范围(index&lt; 0 || index&gt; = size())

请参见Java doc:
https://docs.oracle.com/javase/8/docs/api/java/java/list.html#add-int-int-e-e--

You can't add to index not in list range:

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

See java doc:
https://docs.oracle.com/javase/8/docs/api/java/util/List.html#add-int-E-

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文