Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Arraylist removing element

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Arraylist removing element

    Hi

    i have an arraylist thats keeping a track of members, i need to remove a member and have the list push all other elements down by 1. Please help

     
    ArrayList<Membership> member;
     ArrayList<Books> book;
     private int nextMemberID;
     private int nextBookId;
        /**
         * Constructor for objects of class Library
         */
        public Library() 
        {
     
           nextMemberID = 1;
           nextBookId = 1;
     
           member = new ArrayList<Membership>();
           book = new ArrayList<Books>();
     
     
        }
     
     
     
     
     
    public void removeMember(int ID) {
        //First we find the lot with the given number
        Membership membership = getMember(ID);
        if (membership != null) {
            //Then we can use the method remove with lot as argument
           member.remove(membership);
           nextMemberID--;
     
        }
     
    }


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist removing element

    sorry i didn't mean the element because they do move down as you remove objects, i meant to get my "nextMemberID" to shuffle down by 1 so as to fill the gap.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Arraylist removing element

    I'm not sure what the question really is...it seems your code removes an element, and decrements a marker. I'd suggest an SSCCE to more clearly demonstrate the behavior you get vs the behavior you want.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist removing element

    Ok i can see my question isn't clear so i'll try to explaine. if i add 10 members to the array then the array will hold these members in cells 0-9. My "nextMemberID" will hold these members as 1-10.

    If i remove a member (say member 3) then the array will hold them as 0-8 but my "nextMemberID" will have a gap in the number line where 3 was

    e.g 1-2 4-10.

    Now my nextMemberID-- means that the next member i input will go into array cell 9 but nextMeberID cell 10 which is already in use.

    Hope this helps.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Arraylist removing element

    Still confused so you'll have to clarify with either a better code example or description. I have no idea what your nextMemberID is used for (its an int, so how can it have a gap?), but based upon its name and description presume all you want it to do is represent member.size() + 1 ?

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist removing element

    Here is how i add a member, When i add a member it adds one to the nextMemberID int. So if i add member 4 to the list of 3 members then i'll have 4 members 1,2,3,4. If i remove member 2 then i'll have 1,3,4 but i dont want it to hold the numbers like this, i need the numbers to move down so when i print them it will have their numbers as 1,2,3.

    if i have 4 members then the next MemberID number will be 5, but if i remove a member then it will drop the next MemeberID number to 4 but i will still have members 1,3,4. So the next time i add a member it will want to add a MemberID of 4 so i end up with 1,3,4,4.

       public void addnewMember( String first, String last, String telephone)
        {
     
        member.add(new Membership(nextMemberID,first,last,telephone));
        nextMemberID++; 
    }


     
    public class Membership
    {
     
        private String firstName;
        private String lastName;
        private String telephoneNumber;
        private int ID;
     
     
        /**
         * Constructor for objects of class Member
         */
        public Membership(int ID, String first, String last, String telephone)
        {
           this.ID = ID;
           this.firstName = first;
           this.lastName = last;
           this.telephoneNumber = telephone;
     
     
        }
     
        /**
         * @return The member's number.
         */
        public int getID()
        {
            return ID;
        }

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist removing element

    all depends on what u want. Do u want the ID to represent the position in list of members? Do u want same ID not to be used twice or more (after removing ID 2 not to have it used again)? Or u want removed ID to be used again?

Similar Threads

  1. Removing objects from a hashSet
    By JohnTor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 03:03 PM
  2. removing stopwords from a word list
    By jessie in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 15th, 2010, 12:02 PM
  3. Removing the middle cell
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2010, 01:35 PM
  4. new arrayList element overwrites all previous
    By twinkletoes in forum Object Oriented Programming
    Replies: 2
    Last Post: April 2nd, 2010, 07:45 AM
  5. [SOLVED] Enhancement in program of removing whitespace from text file
    By John in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 27th, 2009, 09:36 AM