Original Post
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: Code they're using (can't directly copy & paste because of Adobe restrictions): 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]
ArrayList num=new ArrayList();
num.add(new Integer(0)); new Integer(1); num.add(new Integer(0));
num.add(new Integer(4)); num.add(new Integer(2));
num.add(new Integer(5)); num.add(new Integer(0));
num.add(new Integer(3));
num.add(new Integer(0));
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);
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);
}
}