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
Code Java:
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--;
}
}
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.
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.
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.
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 ?
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.
Code Java:
public void addnewMember( String first, String last, String telephone)
{
member.add(new Membership(nextMemberID,first,last,telephone));
nextMemberID++;
}
Code Java:
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;
}
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?