【问题标题】:need to make top 10 strings and int's需要制作前 10 个字符串和整数
【发布时间】:2014-01-05 17:34:49
【问题描述】:

我正在制作游戏插件。我需要从 Config 中获取 10 个值,并将其排在前 10 位。它应该打印前 10 位,例如:

1. Name + points
2. Name + points
.
.
.
10. Name + points

使用下面粘贴的代码,我收到了:

[name1, name2, name3, name4][points1,points2,points3]

它没有像我想要的那样排序。它应该从高到低 [10 个结果]

for (String g : Rpgg.getConfig().getConfigurationSection("Stats").getKeys(false)){
    // Set<String> g = Rpgg.getConfig().getConfigurationSection("Stats").getKeys(false);
    // int[] i1 = new int[]{i};
    int i = Rpgg.getConfig().getInt("Stats."+g+".pkt");
    l.add(i);

    a.add(g);
    Collections.sort(a);

    Collections.sort(l);
    map.put(g,i);
}

sorted_map.putAll(map);
Bukkit.broadcastMessage("lp:"+ "§4G:§6 " + a + "   §4pkt§6 " +  l); 

【问题讨论】:

  • 目前是从最低到最高吗?
  • 它让我从最低到最高。

标签: java arrays string sorting


【解决方案1】:

“它让我从最低到最高。”

如果您目前从最低到最高,并且您希望它以相反的顺序,只需使用

  • Collections.reverseOrder() - 返回一个比较器,该比较器对实现 Comparable 接口的对象集合施加自然顺序的逆向。

在你的情况下:

Collections.sort(a, Collections.reverseOrder());

Collections.reverseOrder() 返回一个Comparator。使用这个以Comparator 作为参数的Collections.sort 重载方法

public static void sort(List list, Comparator c)

编辑 - 每个用户评论

“好的,现在我想将 dat list 更改为 int 和 string。怎么做?”

您应该做的是创建一个具有name 字段和score 字段的类,并使其实现Comparable 之类的

public class User implements Comparable<User> {
    String name;
    int score;

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

    public int compareTo(User user){
        return user.getScore() - this.getScore();
    }

    public String getName(){
        return name;
    }

    public int getScore(){
        return score;
    }

    public String toString(){
        return "Name: " + name + " Score: " + score;
    }
}

然后您可以只拥有User objectsList&lt;User&gt;,您可以对所有内容进行排序,而无需对分数和名称进行排序。

List<User> userList = new ArrayList<User>();
// populate list
Collections.sort(userList);

toString 方法会将结果作为字符串提供给您。只需打印User 对象


试运行

import java.util.Collections;
import java.util.List;

public class UserDemo {

    public static void main(String[] args) {
        List<User> users = new ArrayList<User>();
        users.add(new User("Jim", 4398));
        users.add(new User("James", 9880));
        users.add(new User("Jane", 10238));
        users.add(new User("Jack", 5498));
        users.add(new User("Josh", 6530));
        users.add(new User("John", 12349));
        users.add(new User("Jim", 9438));
        users.add(new User("Jill", 12307));
        users.add(new User("Jimbo", 23454));
        users.add(new User("Jason", 5430));

        Collections.sort(users);

        for (User user : users) {
            System.out.println(user);
        }
    }
}

输出:

Name: Jimbo Score: 23454
Name: John Score: 12349
Name: Jill Score: 12307
Name: Jane Score: 10238
Name: James Score: 9880
Name: Jim Score: 9438
Name: Josh Score: 6530
Name: Jack Score: 5498
Name: Jason Score: 5430
Name: Jim Score: 4398

【讨论】:

  • 好的,现在我想将 dat 列表更改为 int 和 string。该怎么做?
  • 你应该创建一个有 name 字段和 score 字段的类,并使其实现 Comparable。
  • 查看我的 EDIT 以了解我在说什么。
【解决方案2】:

您可以使用包含您的信息的二维数组,而不是 2 个单独的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多