
03-20-2010, 07:08 PM
|
|
Senior Member
|
|
Join Date: Nov 2008
Posts: 105
Rep Power: 0
|
|
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.
Last edited by jigglywiggly; 03-20-2010 at 07:13 PM.
|
|

03-20-2010, 07:14 PM
|
|
Senior Member
|
|
Join Date: Nov 2008
Posts: 105
Rep Power: 0
|
|
|
Oh wait I think I see, because k++, after they did a remove... that is really subtle.
|
|

03-20-2010, 07:18 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
|
|
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, 07:19 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
|
|
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, 07:48 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
|
|
|
If you use an iterator, this would not be a problem.
|
|

03-20-2010, 08:14 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
|
|
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, 08:18 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
|
|
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, 08:22 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
|
|
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, 08:25 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
|
|
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, 08:57 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
|
|
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.
<head explodes>
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>
</head explodes>
</brain cell hurts>
</thinking harder>
</drumming fingers on table>
</all those silly tags closed>
Right?
kind regards,
Jos
Last edited by JosAH; 03-20-2010 at 08:59 PM.
|
|

03-20-2010, 10:18 PM
|
|
Senior Member
|
|
Join Date: Nov 2008
Posts: 105
Rep Power: 0
|
|
|
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, 05:54 AM
|
 |
Senior Member
|
|
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 1,047
Rep Power: 3
|
|
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, 06:02 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
|
|
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, 08:56 AM
|
 |
Senior Member
|
|
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 1,047
Rep Power: 3
|
|
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
Originally Posted by Fubarable
|
|
Great to see you Darryl B!!
|
Good to see you too, Pete! <oops/>
db
|
|

03-21-2010, 08:59 AM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
|
|
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:03 AM.
|
|