Container.remove(int i) 抛出意外的 ArrayIndexOutOfBoundsException

发布于 2024-08-04 20:20:30 字数 647 浏览 6 评论 0原文

我正在尝试从 Java 容器中删除除第一个子组件之外的所有组件。以下代码记录“There are 3”并抛出 ArrayIndexOutOfBoundsException: Array index out of range: 2

int componentCount = cardPanel.getComponentCount();
logger.info("There are " + componentCount);
for (int i = 1; i < componentCount; i++) {
    cardPanel.remove(i);
}

但是,此修改后的版本工作完美:

Component[] components = cardPanel.getComponents();
logger.info("There are " + components.length);
for (int i = 1; i < components.length; i++) {
    cardPanel.remove(components[i]);
}

Container.getComponentCount() 和 Container.remove(int i) 似乎不一致容器中组件的数量。还有其他人遇到过这个问题吗?

I'm trying to remove all but the first child component from a Java Container. The following code logs "There are 3" and throws an ArrayIndexOutOfBoundsException: Array index out of range: 2

int componentCount = cardPanel.getComponentCount();
logger.info("There are " + componentCount);
for (int i = 1; i < componentCount; i++) {
    cardPanel.remove(i);
}

However, this modified version works perfectly:

Component[] components = cardPanel.getComponents();
logger.info("There are " + components.length);
for (int i = 1; i < components.length; i++) {
    cardPanel.remove(components[i]);
}

It appears that the Container.getComponentCount() and Container.remove(int i) can't agree on the number of components in the container. Has anyone else run into this problem?

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

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

发布评论

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

评论(6

往事风中埋 2024-08-11 20:20:30

当您执行 cardPanel.remove(i) 时,组件的数量正在减少。

所以你有 [0, 1, 2],并删除索引 1 处的项目。
现在您有 [0, 2] 并删除索引 2 处的项目,这会引发 ArrayIndexOutOfBoundsException。

修改后的版本可以工作,因为它是从容器中删除实际对象,而不是从索引中删除。

试试这个

int componentCount = cardPanel.getComponentCount();
logger.info("There are " + componentCount);
for (int i = 1; i < componentCount; i++) {
    cardPanel.remove(1);
}

When you're doing cardPanel.remove(i), the number of components is reducing.

So you have [0, 1, 2], and remove item at index 1.
Now you have [0, 2] and remove item at index 2, which throws the ArrayIndexOutOfBoundsException.

The modified version works because it is removing the actual object from the container, rather than working from the index.

Try this instead

int componentCount = cardPanel.getComponentCount();
logger.info("There are " + componentCount);
for (int i = 1; i < componentCount; i++) {
    cardPanel.remove(1);
}
苍风燃霜 2024-08-11 20:20:30

本尼的答案是正确的,但根据容器的不同,它可能效率低下。像 ArrayList 这样的东西并不喜欢多次从前面删除内容,因为它每次都会复制所有其余的数据。

这是一个替代方案:

for (int i = cardPanel.getComponentCount() - 1; i >= 1; i--)
{
    cardPanel.remove(i);
}

基本上,这是从末尾开始并朝着开始的方向努力。

它只会对大型收藏有意义,但值得了解该技术。

Benny's answer is correct, but depending on the container, it may be inefficient. Something like an ArrayList doesn't really like removing things from the front multiple times, because it copies all the rest of the data down each time.

Here's an alternative:

for (int i = cardPanel.getComponentCount() - 1; i >= 1; i--)
{
    cardPanel.remove(i);
}

Basically this starts from the end and works towards the start.

It's only going to be significant for large collections, but it's worth knowing the technique.

单身情人 2024-08-11 20:20:30

索引从 0 开始,三项表示第一项位于索引 0,第二项位于索引 1,第三项位于索引 2。

Indexes begin from 0, three items means the first item is at index 0, second is at index 1 and third and thus last is at index 2.

装迷糊 2024-08-11 20:20:30

Container.remove(int i) 删除索引 i 处的项目

您的代码删除索引 1,2,3 处的项目

Container.remove(int i) removes the item at index i

Your code removes items at 1,2,3

陌路黄昏 2024-08-11 20:20:30

你想做什么?如果您想从容器中删除所有项目,那么 removeAll 是正确的选择。

如果您需要在移除时处理这些项目,那么典型的习惯用法是从头到尾,正如 Jon Skeet 所解释的那样。

除非有必要从 0 到末尾,在这种情况下,您将不断删除第 0 个元素:

for (int i = 0; i < componentCount; i++) {
    cardPanel.remove(0);
}

What are you trying to do? If you want to remove all items from a container then removeAll is the right choice.

If you need to process the items while removing then the typical idiom is go from end to front, as Jon Skeet explains.

Unless it is necessary that you go from 0 to end, in which case you keep removing the 0th element:

for (int i = 0; i < componentCount; i++) {
    cardPanel.remove(0);
}
清秋悲枫 2024-08-11 20:20:30

如果您想根据索引上的某些条件删除项目,最好使用显式迭代器来删除它们,同时保留您自己的索引计数。

例如,删除奇数索引处的所有对象:

    int index = 0;
    final Iterator<String> iterator = list.iterator();
    while (iterator.hasNext())
    {
        iterator.next();
        if (index % 2 != 0)
        {
            iterator.remove();
        }
        index++;
    }

虽然这有点长,但它避免了棘手的代码,并且当列表中存在相等的对象时也可以工作。

If you want to remove items based on some condition on their index, it is probably best to use an explicit iterator to remove them while keeping your own count of the index.

For example, to remove all objects at an odd index:

    int index = 0;
    final Iterator<String> iterator = list.iterator();
    while (iterator.hasNext())
    {
        iterator.next();
        if (index % 2 != 0)
        {
            iterator.remove();
        }
        index++;
    }

While this is a bit longer, it avoids tricky code and also works when there are equal objects in the list.

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