Cursor Implementation in Iterator in Java collecti

2019-05-29 01:16发布

All,

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();
}

}

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)

My Question lies in the below line,

while(itr.hasNext())
  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.

  2. 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].

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. Can you please explain what is lastRet? does it has anywhere related to my Questions?
  2. Cursors are always pointed to index[0], which means first element in the array List. Please clarify my understanding.

Regards, Cyborgz

2条回答
小情绪 Triste *
2楼-- · 2019-05-29 01:50

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

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

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() 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 my program, lastRet = -1 and i = 0; this it will return elementData[0] which is "Hello"

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

  3. The Cursor are always set to '0' as its default.
查看更多
狗以群分
3楼-- · 2019-05-29 01:55

hasNext() will return true the first time you call it (before ever calling next()), since the next element is the first element of the list, and your list has a single element.

The first time you call itr.next(), it returns the first element of your list.

cursor is the index of the next element to be returned by a call to next():

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

lastRet is the index of the last element that was returned (by the last call to 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;
查看更多
登录 后发表回答