I have a question on linkedlist iterator
If I'm using next , previous and remove methods
for example :
name.add("Alvin")
name.add("Keven")
name.add("Jack")
ListIterator<String> iterator = name.listIteraot(); //|AKJ
iterator.next(); // A|KJ
iterator.next(); // AK|J
iterator.add("Nina") // AKN|J
iterator.next(); // AKNJ|
iterator.remove(); // AKN|
In the next and then remove method we remove the element before the iterator pointer as the example shows
PUT
I'm confused with the previous and then remove for example
myLList.add("Mary");
myLList.add("John");
myLList.add("Sue");
ListIterator<String> iterator = myLList.iterator
itorator.next();
itorator.next();
itorator.add("Robert");
itorator.pervios();
itorator.pervios();
itorator.remove();
System.out.println(myLList);
the answer for that will be [Mary , Robert , Sue ]
how do we remove for the previous! Do we remove the element on the right?
Because I thought that the removal will be always going backward not forward
my question is how do we remove after using previous , do we remove the element after the iterator pointer or before?
From the documentation of ListIterator#remove
:
Removes from the list the last element that was returned by next() or previous() (optional operation). This call can only be made once per call to next or previous. It can be made only if add(E) has not been called after the last call to next or previous.
In other words, you don't remove after or before the pointer. The documentation specify that the remove
method is regardless of the pointer's position and will remove the object returned by the last call to next/previous.
However, there still exist a pointer in the ListIterator
which lies between element as described at the top of the ListIterator
documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.