简体   繁体   中英

Sorting an array of integers in descending order and relating it to its corresponding string array

I am trying to output the names and corresponding scores in descending order. Having an array of strings and another array of integers, I am trying to relate the two arrays. I used Arrays.sort and tries to get the indices. The indices is then to be used to arrange the names in similar location as the corresponding scores. I have this code but I get run time error saying unfortunately, your app has stopped . Can anyone please help me on what to be done to achieve my goal here? Thank you so much!

           int score[]= new int[4];
            score[0]= 10;
            score[1]= 50;               
            score[2]= 20;
            score[3]= 60;
           String names[] = new String[4];
            names[0]= "player1";
            names[1]= "player2";
            names[2]= "player3";
            names[3]= "player4";

            Arrays.sort(score);
            int index_array[] = new int[4];
            int m = 0;
            for(m = 0; m <4; m++){
                index_array[m]=Arrays.binarySearch(score ,score[m]);
                l = index_array[0];


            }
            for(int i = 0; i<4; i++){
                if(l == score[i]){
                    j = i;
                }
            }
            String name = names[m];
            show.setText(name + " " + Integer.toString(l));

you need to associate the score and the user name. Currently, you are associating them by array index. when you sort the scores, the indices of the scores will change.

Try something like this:

class Score implements Comparable<Score>
{
  int score;
  String player;

  public Score(int theScore, String thePlayer)
  {
    score = theScore;
    player = thePlayer;
  }

  public int compareTo(Score)
  {
    ... compare based on the int score value ...
  }

  ... getters.  setters optional ...
}

List<Score> scoreList = new ArrayList<Score>();

... fill scoreList with Score objects. ...

Collections.sort(scoreList);

Create Player model which holds player's name and score and then use Comparator to sort players by score.

Player model:

class Player {

private String name;

private int score;

public Player(String name, int score) {
    this.name = name;
    this.score = score;
}

public String getName() {
    return name;
}

public int getScore() {
    return score;
}

public String toString() {
    return "name=" + name + "; score=" + score;
}

}

Comparator:

class PlayerComparator implements Comparator<Player> {
public int compare(Player p1, Player p2) {
    return p1.getScore() < p2.getScore() ? -1
            : p1.getScore() > p2.getScore() ? 1 : 0;
}

}

And an example of usage:

public class PlayerTest {

public static void main(String[] args) {
    int score[]= new int[4];
    score[0]= 10;
    score[1]= 50;
    score[2]= 20;
    score[3]= 60;

    String names[] = new String[4];
    names[0]= "player1";
    names[1]= "player2";
    names[2]= "player3";
    names[3]= "player4";

    Player[] players = new Player[names.length];
    for(int i = 0; i < names.length; i++) {
        players[i] = new Player(names[i], score[i]);
    }

    Arrays.sort(players, new PlayerComparator());

}

}

This is a design smell. You shouldn't have two parallel arrays. Instead, you should have a single array of Player objects, where each Player would have a name and a score.

Storing the arra of players by name or by score would then be extremely simple.

Java is an OO language. Use objects.

public class Player
    private final String name;
    private int score;

    public Player(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

...
Player[] players = new Player[4];
players[0] = new Player("player1", 10);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM