# List question, I don't understand why...

• 03-20-2010, 08:08 PM
jigglywiggly
List question, I don't understand why...
﻿
Code:

```private List<Integer> nums;         / ** Precondition: nums.size >0;          */          public void numQuest()         {          int k = 0;          Integer zero = new Integer(0);          while (k< nums.size())          {         if (nums.get(k).equals(zero))          { nums.remove(k);        }  k++; }         }         Assume that List nums initially contains the following          Integer values.          [0, 0, 4, 2, 5, 0, 3, 0]           What will List nums contain as a result of executing numQuest ?          (A) [0, 0, 4, 2, 5, 0, 3, 0]          (B) [4, 2, 5, 3]          (C) [0, 0, 0, 0, 4, 2, 5, 3]          (D) [3, 5, 2, 4, 0, 0, 0, 0]          (e) [0, 4, 2, 5, 3]```
Why is this E, and not B? I mean it starts at 0, is the correct answer wrong? I doubt that, but I don't see anything wrongz in my logic.
I have never used a List before btw, only LinkedLists.
• 03-20-2010, 08:14 PM
jigglywiggly
Oh wait I think I see, because k++, after they did a remove... that is really subtle.
• 03-20-2010, 08:18 PM
JosAH
Quote:

Originally Posted by jigglywiggly
I mean it starts at 0, is the correct answer wrong? I doubt that, but I don't see anything wrongz in my logic.

Well your logic is wrongz (sic). If you have removed an element you go on to the element with the next index. The original element with that index now has the index you just inspected and it won't be tested, so any second element that matches the test will pass untested.

kind regards,

Jos
• 03-20-2010, 08:19 PM
JosAH
Quote:

Originally Posted by jigglywiggly
Oh wait I think I see, because k++, after they did a remove... that is really subtle.

Programming is subtle ... glad you found it yourself.

kind regards,

Jos
• 03-20-2010, 08:48 PM
Fubarable
If you use an iterator, this would not be a problem.
• 03-20-2010, 09:14 PM
JosAH
Quote:

Originally Posted by Fubarable
If you use an iterator, this would not be a problem.

Darn, I never realised this fact; those folks at Sun are indeed really clever. Thanks for pointing it out. (I was already happy when those ListIterators saw the light ;-)

kind regards,

Jos
• 03-20-2010, 09:18 PM
Fubarable
Quote:

Originally Posted by JosAH
Darn, I never realised this fact;

My sarcasm alarm is blasting full tilt. Um, perhaps I should have explicitly stated I was replying to the OP. ;)

Good to see you Jos.
• 03-20-2010, 09:22 PM
JosAH
Quote:

Originally Posted by Fubarable
My sarcasm alarm is blasting full tilt. Um, perhaps I should have explicitly stated I was replying to the OP. ;)

Good to see you Jos.

No seriously, I never thought about that fact (maybe it's because I never remove anything through iterators, I don't know).

I realize that I exhibit myself as the stupid tart that I am again but I don't know who you are; I know you're a regular in the Sun Java forums but ...

kind regards,

Jos (blind twit ;-)
• 03-20-2010, 09:25 PM
Fubarable
Quote:

Originally Posted by JosAH
I realize that I exhibit myself as the stupid tart that I am again but I don't know who you are; I know you're a regular in the Sun Java forums but ...

I'm just an even stupider tart who loves Java and wishes that he knew as much as you.
• 03-20-2010, 09:57 PM
JosAH
Quote:

Originally Posted by Fubarable
I'm just an even stupider tart who loves Java and wishes that he knew as much as you.

Even stupider than me eh? Giving compliments eh? Lemme think ...
<drumming fingers on table>
He can't be Prometheuzz because that whippersnapper loves regular expressions and is quite good at that black art. He even wrote a context free parser for them. Oh the irony, boy, is he stupid, but no ...
<thinking harder>
Jverd? Nah, that guy doesn't have any time left for other forums ...
<brain cell hurts>
Yawmark? No, he is too stupid to come up with another nick name but that stupid? no. My next door neighbour? He is really stupid, he can't tell the difference between a computer and a trash can; well, there hardly is any; but no.
Are you our former Dutch prime minister? No, he can't speak English ...
<Sherlock Holmes mode on>
Are you Encephalopathic by any chance?
</Sherlock Holmes mode on>
</brain cell hurts>
</thinking harder>
</drumming fingers on table>
</all those silly tags closed>

Right?

kind regards,

Jos
• 03-20-2010, 11:18 PM
jigglywiggly
Why is this thread going, I solved it early on... and it's not my code. My code is actually written to make sense :P
• 03-21-2010, 06:54 AM
DarrylBurke
Quote:

Originally Posted by jigglywiggly
Why is this thread going, I solved it early on... and it's not my code. My code is actually written to make sense :P

Hey fuddy-duddy, this is a forum, not a code mill or a homework service. There are social interactions here too.

@Jos: You're good. but do you also remember a nic that ended in 1234 and vanished just after making platinum?

cheers, db
• 03-21-2010, 07:02 AM
Fubarable
Quote:

Originally Posted by Darryl.Burke
@Jos: You're good. but do you also remember a nic that ended in 1234 and vanished just after making platinum?

Oh, THAT guy... he was nothin' but a Duke's whóre. Good riddance I say!

Great to see you Darryl B!!
• 03-21-2010, 09:56 AM
DarrylBurke
Quote:

Originally Posted by Fubarable
Oh, THAT guy... he was nothin' but a Duke's whóre. Good riddance I say!

There's another who's still around, <looks in mirror for confirmation/> but nowadays uses Google less and posts links to lmgtfy.com more ;)

Quote:

Originally Posted by Fubarable
Great to see you Darryl B!!

Good to see you too, Pete! <oops/>

db
• 03-21-2010, 09:59 AM
JosAH
Quote:

Originally Posted by Darryl.Burke
@Jos: You're good. but do you also remember a nic that ended in 1234 and vanished just after making platinum?

My braincells must be leaking, I don't remember that nic ... any reason why he left or was it just a 'mission accomplished' thingy?

kind regards,

Jos