# [java] CS AP Practice Problem (New Problem)

This topic is 4433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I think I'm going to have a series of questions in this area, and I don't want to completely fill the board with closely-related topics... See latest post (relatively) for question OLD POST: I'm doing some last-minute reviewing for the Computer Science AB AP Test tomorrow, and I'm somewhat confused (although it may be due to my lack of consciousness): I'm on problem #3 on this curriculum set, though I had to register to get there, so I don't know if y'all will be able to get there. Anyways, the problem has a simple ArrayList (set by "add()"-ing), and you iterate through that list and remove all Integer values of zero (Integer(0)). I, however, do not understand what is happening (my logic is flawed right now): Basic code for the question: The supplied input is [0 0 4 2 5 0 3 0], and the most logical choice is answer B, [4 2 5 3], but it's not correct. The correct answer they give is C, [0 4 2 5 3]. I've looked through the code and basic documentation for ArrayList.add(), and I'm not finding anything profound. Here's the code I'm compiling:
  ArrayList num=new ArrayList();
System.out.printf("%s\n",num);
int k=0;
Integer zero=new Integer(0);
while (k<num.size()) {
//System.out.printf("[%d] = %d\n",k,num.get(k));
if (num.get(k).equals(zero)) /*{ System.out.printf("  Removed\n");*/ num.remove(k); //}
k++;
}
num.remove(0);
System.out.printf("%s",num);


Code they're using (can't directly copy & paste because of Adobe restrictions):
private ArrayList nums;
public void numQuest() {
int k=0;
Integer zero=new Integer(0);
while (k<nums.size()) {
if (nums.get(k).equals(zero)) nums.remove(k);
}
}


Program Output: [0, 0, 4, 2, 5, 0, 3, 0] [4, 2, 5, 3] I'm compiling with "javac [classname].java", and running with "java [classname]" (no special flags). Is my output being distorted by compiler optimizations??? *Stress'd* [Edited by - deadimp on May 1, 2006 10:32:39 PM]

##### Share on other sites
1. k = 0; list contains [0 0 4 2 5 0 3 0]

2. element 0 equals 0, so remove it.

3. now, the list contains [0 4 2 5 0 3 0]

4. increment k, so now k = 1

5. element 1 equals 4, do not remove it ...

as you can see, the second zero is skipped

##### Share on other sites
At least, that's how their code will behave. The code you wrote has that extra num.remove(0) outside the loop, which is why you are getting a different output.

##### Share on other sites
Ach! I get it now... For some reason I thought that it would magically shift everything after I was done...
And as for that extra "remove()", I had put it there for 'debug' reasons... Forgot to remove it.

Thanks!

##### Share on other sites
NEW QUESTION:
When they talk of speed and such, what all is taken into consideration???
In a question, they have two processes almost identical, which loops from 1 to k and fills "ArrayList someList" with a value of "Integer(k)".
However, one is "someList.add(new Integer(k))" (1) and the other is "someList.add(k,new Integer(k))" (2).
What they want to know is which will be faster, process 1 or 2.
The answer is that they are the same speed, but in my mind I'm thinking of how they would still have to loop somewhere in ArrayList.add(int,Object) to shift the right-sided values to the right, which would take some time, but...

##### Share on other sites
YAY. Im not the only one studying for this monster AB exam. Everybody else in my 30 person class is taking the A exam.

Can someone post some questions on O() notation? Also, a couple questions on Trees. I've already done all the questions I can find.

##### Share on other sites
[Probably a little late by now...]
Try signing up with CollegeBoard AP and looking at the CS AP Curriculum for this year. It has two 'simple' practice tests, one for A and one for AB. Includes some free response questions also...

• 17
• 11
• 12
• 9
• 49
• ### Forum Statistics

• Total Topics
631395
• Total Posts
2999758
×