Easy way to check which player in array list is the highest?

Discussion in 'Plugin Development' started by beanonaboard, Mar 5, 2014.

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

    beanonaboard

    I am wondering if there is an efficient way of checking which player is the highest up e.g. largest y value (furthest from the floor).
     
  2. Offline

    Alshain01

    Code:java
    1. Player highest = some player;// Initialize this somehow or you will get errors.
    2.  
    3. for(Player p : playerList) {
    4. if(p.getLocation().getY() > highest.getLocation().getY()) {
    5. highest = p;
    6. }
    7. }
     
  3. Offline

    beanonaboard


    Thank you what about if I wanted to order the rest of the array list?
    The only way I can think of doing it is actually removing the player who is heightest and carrying on checking the rest of the players
     
  4. Offline

    Alshain01

    Well, in that case you have to take a different approach. The built in ArrayList sort will not use the criteria your want so your gonna have to pull out your advanced data structures book....

    http://en.wikipedia.org/wiki/Bubble_sort
     
  5. Offline

    AoH_Ruthless

    beanonaboard
    Make a player variable called secondHighest (just like highest) and initialize it.

    You will need to edit your for loop to work with two variables : highest player and second highest. (Note: this is in the case of 2 players you are looking for. The more you need, the more variables, the more complex it gets and there are probably better ways of doing this).

    Code:java
    1. Player highest = x;
    2. Player secondHighest = y;
    3.  
    4. for (Player p : your list) {
    5. if (p.getLocation().getY() < highest.getLocation().getY() && p.getLocation().getY() > secondHighest.getLocation().getY()) {
    6. secondHighest = p;
    7. }
    8.  
    9. if (p.getLocation().getY() > highest.getLocation()) {
    10. highest = p;
    11. }
    12. }
     
  6. Offline

    Jombi

    Alshain01 Oh please, a bubble sort is by far one of, if not the worst ways to sort a data structure.

    beanonaboard Look up LinkedLists, stacks, queues, BST's, etc for storing information in order. This makes retrieving the top value MUCH faster. If you're dealing with a lot of players, I'd recommend writing your own class to handle forming an innumerable BST and a few methods to handle searching. That is, if you want scalability and efficiency.
     
  7. Offline

    Codex Arcanum

    It's not the worst. You could be using Bogosort. Or Randall Munroe's FastBogosort.
     
  8. Offline

    Alshain01

    Oh, no I recommended the Bubble Sort for it's ease. It's inefficient but easy to understand. Efficiency won't matter on player list anyway. Takes at least 10000 elements to make a noticeable difference.
     
  9. Offline

    Rocoty

    What's wrong with Collections.sort();?
     
  10. Offline

    Barinade

Thread Status:
Not open for further replies.

Share This Page