0

I have an array and I want to implement FIFO. my project consist of 5 games and a score for each individual game. I had already coded it. heres my code:

if (Game.isNewScore) {
for (int i = 0; i < 5; i++) {
    if (addition[i] == 0) {
        addition[i] = Game.score;
        break;
        }
    }
}

now my problem is when Im already in the sixth game the scoring should replace the score in the 1st game then transfering the scores from left but it seems the score from the game 6 is not showing.

e.g

2 4 6 8 9 - scores

1 2 3 4 5 - no. of Games

when on sixth game

4 6 8 9 (new score)

can someone teach me on this kind of logic? when you seems not getting my point please tell me. Im badly needing a help. thanks

yuki
  • 23
  • 5
  • 1
    use ArrayList ... add element check if size <= desired games count. if not, then remove first element ... just have to say this: it is basic programming problem ... you should know how to do this before starting writing any program – Selvin Feb 12 '15 at 12:00
  • You need to use a FIFO array? Am i right? Have you tried ArrayList? – Saeed Entezari Feb 12 '15 at 12:02
  • http://stackoverflow.com/questions/7266042/java-ring-buffer – barq Feb 12 '15 at 12:09
  • @Selvin please apologize sir if Im not intelligent as you are. btw. thanks – yuki Feb 12 '15 at 12:12

1 Answers1

1

Your best for this would be to change addition for int[] to ArrayList<Integer>. Your code could look something like this then :

ArrayList<Integer> addition = new ArrayList<>();
/*some other code....

*/
    if(Game.isNewScore) {
        if (addition.size() == 5){
            addition.remove(0);
        }
        addition.add(Game.score);
    }
slezadav
  • 6,104
  • 7
  • 40
  • 61