College Discussion

Go Back   College Discussion > College Admissions and Search > SAT and ACT Tests & Test Preparation > AP Tests Preparation
Register FAQ     Search Today's Posts Mark Forums Read

 
Welcome to College Discussion at College Confidential, the Web's leading discussion forum for college admissions, financial aid, SAT prep, and much more! You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, etc. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us.
   College Confidential is dedicated to providing the best free college admissions information available on the Web, through our many articles and this discussion forum.

This welcome message goes away when you register and log in!
Discussion Menu
Discussion Home
Help & Rules
Latest Posts
NEW! College Visits
NEW! Stats Profiles
Top Forums
College Search
College Admissions
Financial Aid
SAT/ACT
Parents
Colleges
Ivy League
Main CC Site
College Confidential
College Search
College Admissions
Paying for College
Sponsors
 Reply
 
Thread Tools
Old 04-27-2008, 01:44 AM   #1
Junior Member
 
Join Date: Apr 2008
Threads: 14
Posts: 32
AP Computer Science AB Question

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

Now element=?
dstrivenmarch is offline  
Old 04-27-2008, 09:11 AM   #2
Junior Member
 
Join Date: Apr 2008
Threads: 4
Posts: 229
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).
Begoner is offline  
Old 05-04-2008, 03:24 AM   #3
Junior Member
 
Join Date: Apr 2008
Threads: 14
Posts: 32
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?
dstrivenmarch is offline  
Old 05-04-2008, 03:47 AM   #4
Junior Member
 
Join Date: Apr 2008
Threads: 14
Posts: 32
Here is an example from Barron I get confused.

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

public static void changeEvenToEmpty (List<String> strList)

{boolean even = true;
ListIterator<String> itr=strList.listIterator();

while(itr.hasNext())

itr.next();

if(even)
itr. set ("");
even=!even;
}
}

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?
dstrivenmarch is offline  
Old 05-04-2008, 08:23 AM   #5
Member
 
Join Date: Apr 2006
Threads: 37
Posts: 557
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.
snipez90 is offline  
Old 05-04-2008, 11:00 AM   #6
Junior Member
 
Join Date: Mar 2008
Threads: 1
Posts: 65
Would you tell me which page is this question on?
Wavvy is offline  
Old 05-04-2008, 11:38 AM   #7
Junior Member
 
Join Date: May 2008
Threads: 6
Posts: 45
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.

As for your second question it will turn String element into "one" although "one" would be more appropriately named "zero."

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).

Code:
public static void changeEvenToEmpty (List<String> strList)
{
         boolean even = true;
         ListIterator<String> itr=strList.listIterator();

         while(itr.hasNext()) {
                itr.next();

                if(even) {
                      itr. set ("");
                      even=!even;
                }
         }
}
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.

Is that what the original question is asking?
pyrotix is offline  
Old 05-04-2008, 12:11 PM   #8
Junior Member
 
Join Date: May 2008
Threads: 6
Posts: 45
Just made a program cause I realized that I've never used listIterator before. Anyways the code:

Code:
import java.util.*;

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);
	}

	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() + " ");
		}
	}


}
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

Code:
Two    ,  Things   ,  That are
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."
pyrotix is offline  
Old 05-04-2008, 12:12 PM   #9
Member
 
Join Date: Apr 2006
Threads: 37
Posts: 557
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.

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.
snipez90 is offline  
Old 05-04-2008, 12:21 PM   #10
Junior Member
 
Join Date: Mar 2008
Threads: 1
Posts: 65
“even indexed objects“ -- bravo!!!!
Wavvy is offline  
Reply


Thread Tools

 


All times are GMT -5. The time now is 01:52 AM.


Copyright 2001-2008, CollegeConfidential.com, Inc., All Rights Reserved
SEO by vBSEO 3.1.0