Java集合中Iterator中的游标实现 [英] Cursor Implementation in Iterator in Java collections

查看:152
本文介绍了Java集合中Iterator中的游标实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

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

Just a beginner to Programming. I was exploring on the java Collections and Iterator and I would like to know how the cursor is used for iterating the collections.

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变量的内部存储器表示如下所示,

As per my Understanding, the internal memory representation of listObj variable looks below,

listObj Representation in memory

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

我的问题在于以下行,

while(itr.hasNext())




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

  1. In Above example, hasNext() returns True. But from my understanding, there is no element in index[1], hence it should return false. But it returns true. please help me to understand this logic.

itr.next()返回值 - Hello。但根据我的理解,它需要返回数组列表中的下一个元素,因为它已经指向索引[0]。

itr.next() returns the value - "Hello". But as per my understanding, it need to return the next element in the Array List since its already pointing to index[0].

另外,我在调试模式下查看了iterator obj,

Also, i have viewed the iterator obj in the debugging mode,

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],这意味着数组列表中的第一个元素。请澄清我的理解。

问候,
Cyborgz

Regards, Cyborgz

推荐答案

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

I have just debugged the entire code and found below solutions,

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

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

This method will return either true or false based on the above logic. the default value of cursor is '0' and its checked against the size of the Array List. i.e., in my case, the hasNext() will return true since Cursor (value=0) != Size ( value=1) and this works well for empty array list as well.


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

  1. itr.next() has below implementations in ArrayList Class,

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

WRT my program, lastRet = -1 and i = 0; this it will return elementData[0] which is "Hello"

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

As said by @Eran lastRet is index of last element returned

这篇关于Java集合中Iterator中的游标实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆