【问题标题】:Cursor Implementation in Iterator in Java collectionsJava集合中迭代器中的游标实现
【发布时间】:2014-11-29 10:36:28
【问题描述】:

全部,

只是编程的初学者。我正在探索 java 集合和迭代器,我想知道游标是如何用于迭代集合的。

public class Collections {

public void myFun()
{
    int i=0;
    List<String> listObj = new ArrayList<String>();
    listObj.add("Hello");
    Iterator<String> itr = listObj.iterator();

    while(itr.hasNext())                         
    {
        String s=(String)itr.next();
        System.out.println(" List Elements are : " +s);
    }
}
public static void main(String[] args) {

    Collections collObj = new Collections();
    collObj.myFun();
}

}

根据我的理解,listObj 变量的内存表示如下所示,

listObj Representation in memory

----------------------------------------
| 45654846  | null   | null | .... | null
----------------------------------------
[0]        [1]       [2]      ...   [10]    
 .
/|\
 |
 |
 |
 itr (Cursor)

我的问题在下面一行,

while(itr.hasNext())
  1. 在上面的例子中,hasNext() 返回 True。但据我了解,index[1] 中没有元素,因此它应该返回 false。但它返回真。请帮助我理解这个逻辑。

  2. itr.next() 返回值 - “Hello”。但据我了解,它需要返回 Array List 中的下一个元素,因为它已经指向 index[0]。

另外,我已经在调试模式下查看了迭代器 obj,

NAME                        VALUE
-----                       -------

itr                         ArrayList$Itr  (id=45)  
    |_  cursor               0  
    |_  expectedModCount     1  
    |_  lastRet              -1 
    |_  this$0               ArrayList<E>  (id=28)  
        |_  [0]            "Hello" (id=40)  
  1. 能否解释一下lastRet 是什么?它是否与我的问题相关?
  2. 游标始终指向索引[0],即数组List 中的第一个元素。请澄清我的理解。

问候, 半机械人

【问题讨论】:

    标签: java collections iterator cursor


    【解决方案1】:

    hasNext() 将在您第一次调用它时返回 true(在调用 next() 之前),因为下一个元素是列表的第一个元素,而您的列表只有一个元素。

    第一次调用 itr.next() 时,它会返回列表的第一个元素。

    cursor 是调用next() 要返回的下一个元素的索引:

    /**
     * Index of element to be returned by subsequent call to next.
     */
    int cursor = 0;
    

    lastRet 是返回的最后一个元素的索引(最后一次调用next()):

    /**
     * Index of element returned by most recent call to next or
     * previous.  Reset to -1 if this element is deleted by a call
     * to remove.
     */
    int lastRet = -1;
    

    【讨论】:

    • 1. “hasNext() 将在您第一次调用它时返回 true(在调用 next() 之前)” - 感谢您的解释。但是,当列表为空时,即使在调用 next() 之前,hasNext() 也会在第一次返回 false。在 java 文档下方“换句话说,如果 {@link #next} 将 * 返回一个元素而不是抛出异常,则返回 {@code true}。”
    • @Cyborgz 我在谈论您的特定代码示例,其中列表不为空,因此第一个 hasNext 不会返回 false。如果列表为 null,则在尝试获取迭代器时会收到 NullPointerException。如果为空,hasNext 将立即返回 false。
    • 非常感谢您在这方面的解释。它很有帮助。
    【解决方案2】:

    我刚刚调试了整个代码,找到了以下解决方案,

    public boolean hasNext() {
                return cursor != size;
            }
    

    此方法将根据上述逻辑返回真或假。 cursor 的默认值为 '0' 并根据 Array List 的大小进行检查。即,在我的情况下,hasNext() 将返回 true,因为 Cursor (value=0) != Size (value=1) 这也适用于空数组列表。

    1. itr.next() 在 ArrayList 类中有以下实现,

      public E next() {
              checkForComodification();
              int i = cursor;
              if (i >= size)
              throw new NoSuchElementException();
          Object[] elementData = ArrayList.this.elementData;
          if (i >= elementData.length)
              throw new ConcurrentModificationException();
          cursor = i + 1;
          return (E) elementData[lastRet = i];
      }
      

      WRT 我的程序,lastRet = -1 和 i = 0;这将返回 elementData[0] 即“Hello”

    2. 正如@Eran lastRet 所说,是返回的最后一个元素的索引

    3. 光标始终设置为默认值“0”。

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多