Get highest score in arraylist

Discussion in 'Plugin Development' started by TumbaBit, Dec 7, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    I have an arraylist of scores and I need to find the highest of them. I've been googling a bit around, but I haven't been able to find anything that actually works with scores and not ints. I hope some of you guys can help me.
  2. Offline


    what do you mean with scores and not ints? integers are used to keep scores...
  3. Offline


    Just iterate through the array. Create a variable that stores the largest value, and then compare the value from your array and the value stored as your max value. If the one from the array is bigger, set the new max value for that. Then once you are done iterating through the whole array, you will have the largest value.
  4. Offline


    This is how I would do it:
    2. private ArrayList<Integer> ints = new ArrayList<Integer>(); // You can add all the ints in the world to this arraylist.

    2. public int getHighestInt() { // method
    3. int highest = 0; // highest number, starting from 0
    4. for(Integer i : ints) { // loop through all the ints in the arraylist
    5. if(highest > i) continue; // if the highest number is already higher than i, continue with the loop
    6. if(highest == i) continue; // if the highest number is equal to i, continue with the loop
    7. if(highest < i) highest = i; // if the highest number is lower than i, then highest = i
    8. }
    9. return highest; // return the highest value in the arraylist
    10. }
    11. [syntax][/syntax]
  5. Offline


    Are you sure? It's an arraylist of the "Score" and whenever I try to treat it as an int I get errors.
  6. Offline


    I assume by Score you mean Bukkit's wrapper class pertaining to Scoreboards. In that case, there should be a method inside of the Score class to get the score as an int.
  7. Offline


    I redesigned my system to work with integers. Also pogo your tutorials really helped me learn the basics. Thank you for making them and helping me here :)
    PogoStick29 likes this.
Thread Status:
Not open for further replies.

Share This Page