Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2010, 07:08 PM
Senior Member
 
Join Date: Nov 2008
Posts: 105
Rep Power: 0
jigglywiggly is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-20-2010, 07:14 PM
Senior Member
 
Join Date: Nov 2008
Posts: 105
Rep Power: 0
jigglywiggly is on a distinguished road
Default
Oh wait I think I see, because k++, after they did a remove... that is really subtle.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-20-2010, 07:18 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by jigglywiggly View Post
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-20-2010, 07:19 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by jigglywiggly View Post
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-20-2010, 07:48 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
Fubarable is on a distinguished road
Default
If you use an iterator, this would not be a problem.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-20-2010, 08:14 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by Fubarable View Post
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-20-2010, 08:18 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by JosAH View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-20-2010, 08:22 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by Fubarable View Post
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 ;-)
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 03-20-2010, 08:25 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by JosAH View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 03-20-2010, 08:57 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by Fubarable View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 03-20-2010, 10:18 PM
Senior Member
 
Join Date: Nov 2008
Posts: 105
Rep Power: 0
jigglywiggly is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 03-21-2010, 05:54 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 1,047
Rep Power: 3
Darryl.Burke will become famous soon enough
Default
Originally Posted by jigglywiggly View Post
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
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 03-21-2010, 06:02 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,429
Rep Power: 11
Fubarable is on a distinguished road
Default
Originally Posted by Darryl.Burke View Post
@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!!
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 03-21-2010, 08:56 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 1,047
Rep Power: 3
Darryl.Burke will become famous soon enough
Default
Originally Posted by Fubarable View Post
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 View Post
Great to see you Darryl B!!
Good to see you too, Pete! <oops/>

db
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 03-21-2010, 08:59 AM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,337
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by Darryl.Burke View Post
@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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with array list jpanel question!!! Plz help!!! helpmeplease New To Java 17 12-11-2009 12:37 PM
n00b: linked list question ankitmcgill New To Java 2 03-13-2009 04:15 AM
[SOLVED] Linked list question ztrath New To Java 8 03-11-2009 11:38 PM
A simple List question right2001 New To Java 2 02-16-2009 03:37 AM
Linked List Question CirKuT New To Java 4 12-10-2008 06:56 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:03 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org