AP Computer Science AB Question

<p>If you have an list that has “One”, “Two”, “Three”, and “Four”, then you use ListIterator<string> itr=list.listIterator(); String element=itr.next();</string></p>

<p>Now element=?</p>

<p>It would return the element at index 0 (“One”). An iterator works in a somewhat peculiar way – its “index” actually lies between two elements in the array. By default, it starts between the beginning of the array and the first element in the array (index -0.5, as it were). Calling next() returns the element to its right and increments its index by 1 (taking it to 0.5). From there, calling previous() would return the element to its left (the same one as before).</p>

<p>If a list has 1,2,3,4,5 and you use ListIterator<integer> itr=list.listIterator(); itr.next(); itr.set(100); Which number has been changed to 100?</integer></p>

<p>Here is an example from Barron I get confused.</p>

<p>// Change every even-indexed element of strlist to the empty string.
// Precondition: strList contains String values.</p>

<p>public static void changeEvenToEmpty (List<string> strList)</string></p>

<p>{boolean even = true;
ListIterator<string> itr=strList.listIterator();</string></p>

<p>while(itr.hasNext())</p>

<p>itr.next();</p>

<p>if(even)
itr. set (“”);
even=!even;
}
}</p>

<p>It is supposed to change even-indexed element, but if itr.next() refers to the first element, and set changes this element, then this method is to change odd-indexed element. Is Barron wrong?</p>

<p>Yeah I think barrons is wrong. The set() method should change the item returned by the last call to next(). All you have to do to fix it is to set the boolean to false in the first line.</p>

<p>Would you tell me which page is this question on?</p>

<p>My understanding: the method turns even indexed objects blank. The first element is at index 0 and is thus going to be made blank. That’s why the boolean is set to true as opposed to false at the beginning.</p>

<p>As for your second question it will turn String element into “one” although “one” would be more appropriately named “zero.”</p>

<p>The problem with the method that I see (not sure if I’mt just seeing things though) is that it will only delete the first even element (index 0). </p>

<p>



public static void changeEvenToEmpty (List<string> strList)
{
         boolean even = true;
         ListIterator<string> itr=strList.listIterator();</string></string></p>

<pre><code>     while(itr.hasNext()) {
            itr.next();

            if(even) {
                  itr. set ("");
                  even=!even;
            }
     }
</code></pre>

<p>}


</p>

<p>As once even is set to false it is never set true again and no other element is set to blank. “The even=!even;” needs to be put outside the if structure.</p>

<p>Is that what the original question is asking?</p>

<p>Just made a program cause I realized that I’ve never used listIterator before. Anyways the code:</p>

<p>



import java.util.*;</p>

<p>class Testing {
    public static void main(String[] args) {
        LinkedList list = new LinkedList<string>();
        list.add("This");
        list.add("List");
        list.add("Is");
        list.add("Built");
        list.add("For");
        list.add("Testing");
        modify(list);
    }</string></p>

<pre><code>private static void modify (LinkedList list) {
    ListIterator iterate = list.listIterator();

    String element = (String)iterate.next();
    System.out.println(element);

    element = (String)iterate.next();
    System.out.println(element);

    iterate.next();
    iterate.next();

    iterate.add("sort of");

    iterate.next();

    iterate.set("for (I think)");
    printList(list);


}

private static void printList (LinkedList list) {
    ListIterator iterate = list.listIterator();
    while(iterate.hasNext()) {
        System.out.print(iterate.next() + " ");
    }
}
</code></pre>

<p>} 

Don't ask why there are three methods :P.

Prints:

This List This List Is sort of Built for (I think) Testing

Interesting to note that adding an element adds it before the pointer. E.g.

in the list



Two    ,  Things   ,  That are


</p>

<p>where list.Next() has just returned “Things” adding “Small” means that now a call to list.Next will still return “That are” but a call to list.previous (which it seems is not in the AP subset… odd) will return “Small.”</p>

<p>pyrotix you are right, dstrivemarch said “It is supposed to change even-indexed element” while I read it as even-ordered elements. In this case, itr.next() would return the object at index 0, and so the method sets even-indexed elements to “”. Good catch there.</p>

<p>EDIT. Yeah the thing you have to remember is that the iterator is between elements (different from pointers). There really isn’t that much logic involved, you just have to keep in mind the cases.</p>

<p>“even indexed objects“ -- bravo!!!</p>