1 2 Previous Next 16 Replies Latest reply on Mar 19, 2007 3:51 PM by 807599

    can not add a member to arraylist

    807599
      Hey

      I have the following code that looks through an arraylist of members and searches for a member in the arraylist that has the same ID as a member that will be added to the arraylist. If the new member does not have the same ID as the IDs of the other members in the arraylist, it will add the new member to the arraylist, and if it does, it will print a message saying that it is not possible to have 2 members in the arraylist with the same ID:

      public void addMember(Member newMember)
      {
      Iterator<Member> it = members.iterator();
      while(it.hasNext()) {
      Member member = it.next();
      if(member.getID().equals(newMember.getID())) {

      System.out.println("Sorry, the ID already exists in the libaray");
      }
      else {
      members.add(newMember);

      The issue is that im not able to add the new member to the arraylist if the new member's ID is not the same as the other Member IDs in the arraylist. When i check the size of the arraylist with size.(), it just returns the value 0.

      Not sure why it wont add the new member. Anyone got an idea whats happening here?

      Message was edited by:
      XtremeHound
        • 1. Re: can not add a member to arraylist
          807599
          you can't modify a collection while iterating over it like that, and, and there's no need to, here, either. use the contains() method to see if your member is already in the list, if not, add it
          • 2. Re: can not add a member to arraylist
            807599
            I want to be able to see whether the same ID already exists in the arraylist but not the same object.
            • 3. Re: can not add a member to arraylist
              807599
              use the contains() method to see if your member is
              already in the list, if not, add it
              As soon as the Member class implements the equals method by Id comparison (which in fact sounds like a good equals implementation to me.)

              But if it is not the case, you'll have to iterate through your whole list to make sure it doesn't contain a member with same ID before you can decide to add the new one.
              • 4. Re: can not add a member to arraylist
                807599
                don't do the add inside the loop, then. use a boolean variable to say whether you found it or not

                or, if ID really is a unique ID, you could give your Member class an equals method that uses the ID to test for equality
                • 5. Re: can not add a member to arraylist
                  807599
                  Not sure how to implement a boolean variable in the current code that i have. What part of the code would i need to modify in order to do so?
                  • 6. Re: can not add a member to arraylist
                    807599
                    Not sure how to implement a boolean variable in the
                    current code that i have. What part of the code would
                    i need to modify in order to do so?
                     public void addMember(Member newMember)
                    {
                    Iterator<Member> it = members.iterator();
                    boolean found = false; // here's the missing piece
                    while(it.hasNext()) {
                    Member member = it.next();
                    if(member.getID().equals(newMember.getID())) {
                    found = true;
                    System.out.println("Sorry, the ID already exists in the libaray");
                       }
                    }
                      if ( !found) {
                         members.add(newMember);
                      }
                    }
                    as both Tim and I suggested, though, you could avoid the iterating altogether by making the equals method use IDs for checking, and using Collection.contains. what type is "members"? you may need to override hashCode, too, which actually you should do when you override equals, regardless
                    • 7. Re: can not add a member to arraylist
                      807599
                      Thanks for your help. I will have to see if the code with the implemented boolean variable works later. Will let you know .
                      • 8. Re: can not add a member to arraylist
                        807599
                        I tried the code with the implemented boolean variable but when i run size.() for the arraylist, it still shows 0.
                        • 9. Re: can not add a member to arraylist
                          807599
                          I tried the code with the implemented boolean
                          variable but when i run size.() for the arraylist, it
                          still shows 0.
                          it's post-some-code time, then
                          • 10. Re: can not add a member to arraylist
                            807599
                            Its alright, you dont have to. I have to avoid making use of booleans in loops at the moment as it is not introduced in a practical exercise that im doing. It would appear that i have gone a step ahead otherwise. Thank you for your time though, much appreciated.
                            • 11. Re: can not add a member to arraylist
                              807599
                              Its alright, you dont have to. I have to avoid making
                              use of booleans in loops at the moment as it is not
                              introduced in a practical exercise that im doing. It
                              would appear that i have gone a step ahead otherwise.
                              Thank you for your time though, much appreciated.
                              I meant you post some code!
                              • 12. Re: can not add a member to arraylist
                                807599
                                oh lol. do you want the code with the boolean variable?

                                public void addMember(Member newMember)
                                {
                                Iterator<Member> it = members.iterator();
                                boolean found = false; // here's the missing piece
                                while(it.hasNext()) {
                                Member member= it.next();
                                if(member.getID().equals(newMember.getID())) {
                                found = true;
                                System.out.println("Sorry, the ID already exists");
                                }

                                if ( !found) {
                                members.add(newMember);
                                }
                                }
                                }

                                Message was edited by:
                                XtremeHound
                                • 13. Re: can not add a member to arraylist
                                  807599
                                  Its alright, you dont have to.
                                  What a kind answer ;-)
                                  I have to avoid making use of booleans in loops
                                  Doesn't this sentence sound kind of weird ?
                                  It would appear that i have gone a step ahead otherwise
                                  You can't even imagine the consequences.

                                  Tim - thanks for that post
                                  • 14. Re: can not add a member to arraylist
                                    807599
                                    What a kind answer ;-)
                                    yeh, bit of a misunderstanding, i thought george ment that he was going to suggest another code
                                    Doesn't this sentence sound kind of weird ?
                                    what this means is that im currently doing a practical worksheet where im suppose to give solutions to answers, but im not suppose to use code that i havent learnt yet
                                    You can't even imagine the consequences.
                                    again, im not suppose to use code that i havent learnt yet so i dont want to add code that i havent come across yet which is not related to the topic of the practical worksheet lol
                                    1 2 Previous Next