Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>No they do not do the same.</p> <pre><code>while (ridesIterator.hasNext()) { ridesIterator.next(); Corsa previous = ridesIterator.previous(); //rides.get(i-1) Corsa current = ridesIterator.next(); //rides.get(i) </code></pre> <p>The variables previous and current would contain the same "Corsa" value, see the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ListIterator.html" rel="nofollow">ListIterator documentation</a> for details (iterators are "in between" positions).</p> <p>The correct code would look as follows:</p> <pre><code>while (ridesIterator.hasNext()) { Corsa previous = ridesIterator.next(); //rides.get(i-1) if(!ridesIterator.hasNext()) break; // We are already at the last element Corsa current = ridesIterator.next(); //rides.get(i) ridesIterator.previous(); // going back 1, to start correctly next time </code></pre></li> <li><p>The code would actually look exactly the same, only the interpretation (as shown in the comments) would be different:</p> <pre><code>while (ridesIterator.hasNext()) { Corsa previous = ridesIterator.next(); //rides.get(i) if(!ridesIterator.hasNext()) break; // We are already at the last element Corsa current = ridesIterator.next(); //rides.get(i+1) ridesIterator.previous(); // going back 1, to start correctly next time </code></pre></li> </ol> <p>From a (premature?) optimization viewpoint the ListIterator implementation is better. </p> <ul> <li>LinkedList is a doubly-linked list which means that each element links to both its predecessor (previous) as well as its successor (next). So it does 3 referals per loop. => 3*N</li> <li>Each get(i) needs to go through all previous elements to get to the i index position. So on average N/4 referals per loop. (You'd think N/2, but LinkedList starts from the beginning <strong>or</strong> the end of the list.) => 2 * N * N/4 == N^2 /2</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload