Results 1 to 15 of 15
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

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

    
    Java 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.

  2. #2
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

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

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote 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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote 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 ;-)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote 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.

  11. #11
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    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

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote 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

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote 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!!

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote 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 ;)

    Quote Originally Posted by Fubarable View Post
    Great to see you Darryl B!!
    Good to see you too, Pete! <oops/>

    db

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote 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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •