Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the first call to previous returns the same element as the last call to next?

I'm reading Java Tutorial Oracle on List interface and have encountered the following statement:

Calls to next and previous can be intermixed, but you have to be a bit careful. The first call to previous returns the same element as the last call to next.

I'm struggling to get my head around the phrase: The first call to previous returns the same element as the last call to next. Could someone explain it in a bit more detail?

like image 934
Thor Avatar asked Jan 01 '26 03:01

Thor


1 Answers

The first call to previous returns the same element as the last call to next. Could someone explain it in a bit more detail?

If you say .next() and then .previous(), you get the same instance because of the internal position of the Iterator. Otherwise, consecutive calls to .next() would return the same instance.

a b c
  ^

.next() gives b, but moves the pointer to c

a b c
    ^

so .previous() then yields b.

like image 191
Elliott Frisch Avatar answered Jan 03 '26 15:01

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!