操作 ArrayList 时出现 AbstractList.remove() 中的 UnsupportedOperationException

发布于 2024-11-14 08:00:27 字数 613 浏览 1 评论 0原文

ArrayList 的列表迭代器确实实现了删除方法,但是,我抛出以下异常:

UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144)

通过此代码:

protected void removeZeroLengthStringsFrom(List<String> stringList)
{
    ListIterator<String> iter = stringList.listIterator();
    String s;
    while (iter.hasNext())
    {
        s = iter.next();
        if (s.length() == 0)
        {
            iter.remove();
        }
    }
}

我在这里缺少什么?我已经验证我传入的 List 确实是 ArrayList

谢谢!

ArrayList's list iterator does implement the remove method, however, I get the following exception thrown:

UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144)

By this code:

protected void removeZeroLengthStringsFrom(List<String> stringList)
{
    ListIterator<String> iter = stringList.listIterator();
    String s;
    while (iter.hasNext())
    {
        s = iter.next();
        if (s.length() == 0)
        {
            iter.remove();
        }
    }
}

What am I missing here? I have verified that the List<String> I am passing in are indeed ArrayList<String>.

Thanks!

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

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

发布评论

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

评论(2

一笔一画续写前缘 2024-11-21 08:00:27

我认为您可能正在使用Arrays实用程序来获取传递给该方法的List。该对象确实是 ArrayList 类型,但它是 java.util.Arrays.ArrayList,而不是 java.util.ArrayList

java.util.Arrays.ArrayList 版本是不可变的,并且其 remove() 方法不会被重写。因此,它遵循 remove()AbstractList 实现,该实现会抛出 UnsupportedOperationException

I think you may be using the Arrays utility to get the List that you pass into that method. The object is indeed of type ArrayList, but it's java.util.Arrays.ArrayList, not java.util.ArrayList.

The java.util.Arrays.ArrayList version is immutable and its remove() method is not overridden. As such, it defers to the AbstractList implementation of remove(), which throws an UnsupportedOperationException.

灯角 2024-11-21 08:00:27

我怀疑您正在传递 ArrayList,因为 ArrayList 迭代器上的删除方法不会引发该异常。

我猜你正在传递一个 ArrayList 的用户派生类,该类的迭代器在删除时会抛出该异常。

public void remove() {
    if (lastRet == -1)
    throw new IllegalStateException();
        checkForComodification();

    try {
    AbstractList.this.remove(lastRet);
    if (lastRet < cursor)
        cursor--;
    lastRet = -1;
    expectedModCount = modCount;
    } catch (IndexOutOfBoundsException e) {
    throw new ConcurrentModificationException();
    }
}

I doubt you are being passed an ArrayList, as the remove method on the ArrayList iterator does not throw that exception.

I'm guessing your are being passed a user derived class of ArrayList who's iterator does throw that exception on remove.

public void remove() {
    if (lastRet == -1)
    throw new IllegalStateException();
        checkForComodification();

    try {
    AbstractList.this.remove(lastRet);
    if (lastRet < cursor)
        cursor--;
    lastRet = -1;
    expectedModCount = modCount;
    } catch (IndexOutOfBoundsException e) {
    throw new ConcurrentModificationException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文