简体   繁体   English

按降序对整数数组进行排序,并将其与相应的字符串数组相关联

[英]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. 我使用Arrays.sort并尝试获取索引。 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. 相反,您应该有一个Player对象数组,其中每个Player都有一个名称和一个分数。

Storing the arra of players by name or by score would then be extremely simple. 然后,通过名称或分数存储玩家的角色将非常简单。

Java is an OO language. Java是一种OO语言。 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);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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