Algorithms and OOD (CSC 207 2014F) : EBoards

CSC 207.01 2014F: Extra Session 8 (Thursday, 30 October 2014)


============================================================-

Topics to discuss

Lab: Problem 1

a. Where, conceptually, is an iterator relative to the elements of a list?

Between elements (or before the first or after the last).

    a b c
       ^            // Between b and c
     ^              // Between a and b
         ^          // After all the elements
   ^                // Before all the elements

The cursor starts before the first element

b. What element does remove remove? For example, if an iterator, it, is between the a and the b in the list [a,b,c] and we call it.remove(), which value does it remove?

    a b c
     ^              // Between a and b

"It gets rid of b" ? Argument: ?

"It gets rid of a" ? Argument: It's the thing that was most recently returned by next.

But what if we'd just called previous? Then it gets rid of b.

But we have an iterator, not a list iterator, so there is no previous.

c. Suppose we have a list iterator, lit, and call lit.add(x) and then immediately after call lit.add(y). In what order will x and y appear in the list?

    a b c
     ^              // Between a and b
    a x b c         // After inserting x
       ^

Why is it after x? Because the call to next should not change.

    a x b c         // After inserting x
       ^
    a x y b c         // After inserting y
         ^

d. Suppose we have a list iterator, lit, between the a and b in the list [a,b,c]. Suppose we then call lit.remove() and then immediately after call lit.remove() again. What can or should happen?

    a b c
     ^              // Between a and b

Let's assume that we remove the b because we were moving backwards.

    a c
     ^              // Between a and c

Since remove is supposed to remove the thing we just saw, and we lost the thing we just saw on the remove, we cannot call it a second time.

e. Can we add an element using a list iterator immediately after creating that list iterator? Why or why not?

Can I do this? Should I be able to do this?

    void whatever(SimpleList<Integer> vals)
    {
      ListIterator<Integer> lit = vals.iterator();
      lit.add(5);
    } // whatever

Yes. We add it to the beginning. It's necessary, because you have to be able to add to the empety list.

f. Can we remove an element using a list iterator immediately after creating that list iterator? Why or why not?

Senseless, given that remove "[r]emoves from the list the last element that was returned by next() or previous() "

g. Can we set an element using a list iterator immediately after creating that list iterator? Why or why not?

Senseless. You need an element to set. Note that set requires a next or a previous.

h. Suppose we've created two list iterators, lit1 and lit2 for the list [a,b,c], and both are between the a and the b in the list. If we call lit1.add(d), what should lit2.next() return?

    a b c
     ^              // lit1
     ^              // lit2

Add

    a d b c
       ^              // lit1
     ? ?              // lit2

"b. It was right before b before. It's still right before b."

"It might depend on what direction you are moving in the list."

"If we're moving forward through the list, it should return d because it was right after a."

    Exception in thread "main" java.util.ConcurrentModificationException
            at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886)
            at java.util.ArrayList$Itr.next(ArrayList.java:836)
            at taojava.lists.ArrayListExperiment.main(ArrayListExperiment.java:59)

Answer: If you modify the list during iteration, you invalidate every other iterator. (All the documentation says is that lit2 is "invalid". But good practice is to "fail fast".)

i. Suppose we've created two list iterators, lit1 and lit2 for the list [a,b,c], and both are between the a and the b in the list. If we call lit1.remove(), what should lit2.next() return?

Same answer. We've modified the list. It's not defined.

Let's look at fail fast

    public class SimpleArrayList
    {
      long mods;

      ListIterator<String> lit = new ListIterator<String>()
        {
          long mods;

          this.mods += 1;    // changes the mods field in the ListIterator
          SimpleArrayList.this.mods += 2;
                            // Changes the mods field in the particular
                            // SimpleArrayList that contains this 
                            // iterator.


        }; // lit, an anonymous inner class
    } // class SimpleArrayList