I've got a Java program which stores a list of cricket club players using an arraylist. In the club class I have a static field which keeps track of of how many players are registered in the club. Also I assign a player ID to the static field so that each time a player is added he gets an ID. This is how
id = regPlayer++;
regPlayer is the static field, which is initialized as 1
The problem I'm having is that if I want to remove a player from the arraylist I can't seem to be able to update the IDs. For example if I have four players they will have the following IDs before I remove any of them:
Player 1 ID = 1
Player 2 ID = 2
Player 3 ID = 3
Player 4 ID = 4
If I remove Player 2 I now want the third player to have ID 2 and the fourth player to have ID 3, like that:
Player 1 = 1
Player 3 = 2
Player 4 = 3
Is there a way of achieving this with arraylists and how?
playerIdis bound to the index of the player in the array you could just use the element's index asplayerId.