Created
March 27, 2015 09:01
-
-
Save kvalv/ca0fe72dcaee9690fe37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package oving8.highscore; | |
| import java.util.List; | |
| import java.util.ArrayList; | |
| import oving8.highscore.HighscoreListener; | |
| import oving8.highscore.HighscoreListListener; | |
| public class HighscoreList { | |
| private int maxSize; | |
| ArrayList <Integer> results = new ArrayList <Integer> (); | |
| HighscoreList ( int maxSize ) { | |
| if ( maxSize > 0 ) { this.maxSize = maxSize; } | |
| else { this.maxSize = 5; System.out.println("invalid input. Sat maxsize to 5 by default"); } | |
| } | |
| int size() { | |
| return results.size(); | |
| } | |
| int getElement ( int index ) { | |
| if ( index >= results.size() - 1 ) { | |
| return -1; | |
| } | |
| return results.get( index ); | |
| } | |
| void addResult ( int result ) { | |
| if ( results.isEmpty() ) { | |
| results.add( result ); | |
| } else { | |
| if ( result < results.get ( results.size() - 1 ) || results.size() < maxSize ) { | |
| int i = 0; | |
| while ( results.get(i) < result ) { | |
| i += 1; | |
| if ( results.size() == i ) { break; } | |
| } results.add (i, result ); | |
| } | |
| } | |
| while ( results.size() > maxSize ) { | |
| results.remove( results.size() - 1 ); | |
| } | |
| } | |
| void addHighscoreListListener ( HighscoreListListener listener ) { | |
| } | |
| void removeHighscoreListListener ( HighscoreListListener listener ) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment