【问题标题】:Java: Which CLASS does the object returned by list.listIterator() belong to?Java:list.listIterator() 返回的对象属于哪个类?
【发布时间】:2016-02-08 16:13:25
【问题描述】:

关于 Java 中的迭代的问题。我(有点)熟悉 Iterator、ListIterator 和 Iterable 接口,即我明白它们背后的想法。但这也是我的问题所在。

如果我有一个 ArrayList 的实例,让我们简单地称这个实例为“list”,然后如果我让方法调用“list.listIterator()”,那么生成的(即返回的)对象属于哪个 CLASS?

我知道它必须是一个实现接口 ListIterator 的类,但这仍然不能告诉我它属于哪个实际的特定类。在线文档似乎也没有告诉我这一点。或者它只是一个“内部”——因此是匿名/未命名的——Java 类?

谢谢! 荷兰。

【问题讨论】:

    标签: java list class iteration iterable


    【解决方案1】:

    你可以通过做找到

    System.out.println(new ArrayList<String>().listIterator().getClass());
    

    您会看到该类在ArrayList 中声明,并被称为ListItr

    它是private。这样做有充分的理由。首先,它使ArrayList 的作者能够在不破坏任何人代码的情况下更改实现。此外,您无需关心实际课程是什么;重要的是它遵守ListIterator 的合同。

    【讨论】:

    • 好吧,我认为这回答了这个问题(并且还教/提醒了我一些事情,即使用 getClass)。非常感谢!
    • 会尽快接受答案(有等待时间)。
    • 没问题。很高兴我能帮上忙。
    【解决方案2】:

    在线文档告诉您可以从 API 中获得什么以及您可以做什么,您可以查看源代码以找到所需的详细信息,因此您可以这样做:

    来自 Java 源代码:

    public ListIterator<E> listIterator(int index) {
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException("Index: "+index);
        return new ListItr(index);
    }
    

    上面告诉你将得到ListItr的实现,下面是类的实际实现:

     private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            super();
            cursor = index;
        }
    
        public boolean hasPrevious() {
            return cursor != 0;
        }
    
        public int nextIndex() {
            return cursor;
        }
    
        public int previousIndex() {
            return cursor - 1;
        }
    
        @SuppressWarnings("unchecked")
        public E previous() {
            checkForComodification();
            int i = cursor - 1;
            if (i < 0)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i;
            return (E) elementData[lastRet = i];
        }
    
        public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();
    
            try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    
        public void add(E e) {
            checkForComodification();
    
            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2014-06-14
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      相关资源
      最近更新 更多