user3204315
user3204315

Reputation: 15

Moving all array values for one index

One word: Highscores. And Java.

Top 5 highscores for my game are stored in ArrayList of 5 indexes, I seem to understand everything except moving all elements for one index up. For example: A new player has more points than the previously ranked 1st player, so he replaces him. Now the previously first player is second, the second is third and so on.

Upvotes: 0

Views: 177

Answers (1)

swyrazik
swyrazik

Reputation: 56

If I understood correctly your situation, the following code should do what you are asking for:

ArrayList<Integer> highscores = new ArrayList<>();

//...add elements to array 

int newHighscore = 1000;

/* add the new highscore to the first index of the array and automatically
   increment the indices of the elements that are after it */
highscores.add(0, newHighscore); 

//remove the last highscore from the list
highscores.remove(highscores.size() - 1);

If you want a more detailed example, I can expand on it.

Upvotes: 1

Related Questions