尝试对Java对象数组进行排序
我对Java相当陌生(C++和C是我常用的语言) 我试图找出如何对一组对象进行排序。我需要让用户输入有多少球员,球员的名字和球员的分数。然后程序将从上到下输出分数和球员姓名 我让用户输入他们的信息,并将其存储到一个玩家类中 我不知道如何对球员的得分进行排序。我很确定我需要使用一个可比的,但就我的生活而言,我不知道如何设置它 有人能帮忙吗 我知道player类中的代码不正确,哈哈尝试对Java对象数组进行排序,java,arrays,sorting,Java,Arrays,Sorting,我对Java相当陌生(C++和C是我常用的语言) 我试图找出如何对一组对象进行排序。我需要让用户输入有多少球员,球员的名字和球员的分数。然后程序将从上到下输出分数和球员姓名 我让用户输入他们的信息,并将其存储到一个玩家类中 我不知道如何对球员的得分进行排序。我很确定我需要使用一个可比的,但就我的生活而言,我不知道如何设置它 有人能帮忙吗 我知道player类中的代码不正确,哈哈 import java.util.*; import java.util.Arrays; public class
import java.util.*;
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner input1 = new Scanner(System.in);
int allPlayers;
int index[] = new int[12];
int i= 0;
System.out.print("Please enter the number of players");
allPlayers = input.nextInt();
Player[] playersArray = new Player[allPlayers];
for(i = 0; i <allPlayers; i++){
playersArray[i] = new Player();
System.out.print("What is the name of Player # " + (i+1) +"?");
playersArray[i].name = input1.nextLine();
System.out.print("What was the score of Player # " + (i+1) + "?");
playersArray[i].score = input.nextInt();
}
System.out.print(playersArray[i].name);
System.out.print(playersArray[i].score);
}
}
public class Player implements Comparable<Player> {
private int score; // players score
private String name; // players name
public Player(int score, String name){
this.core = score;
this.name = name;
}
public int compareTo(Player other){
int last = this.score.compareTo(other.score);
return last == 0 ? this.name.compareTo(other.score) : score;
}
}
import java.util.*;
导入java.util.array;
公共类HelloWorld{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
扫描仪输入1=新扫描仪(System.in);
国际球员;
整数索引[]=新整数[12];
int i=0;
系统输出打印(“请输入玩家数量”);
allPlayers=input.nextInt();
Player[]playersArray=新玩家[所有玩家];
对于(i=0;i蛋糕行走。做了一些简化和清理
class Player implements Comparable<Player> {
public final int score; // players score
public final String name; // players name
public Player(final int score, final String name) {
this.score = score;
this.name = name;
}
@Override public int compareTo(final Player other) {
return other.score - this.score;
// return this.score - other.score; // or this to reverse order
}
}
public class PlayerSorting {
public static void main(final String[] args) {
try (final Scanner input = new Scanner(System.in);//
final Scanner input1 = new Scanner(System.in);) {
System.out.print("Please enter the number of players");
final int allPlayers = input.nextInt();
final Player[] playersArray = new Player[allPlayers];
for (int i = 0; i < allPlayers; i++) {
System.out.print("What is the name of Player # " + (i + 1) + "?");
final String name = input1.nextLine();
System.out.print("What was the score of Player # " + (i + 1) + "?");
final int score = input.nextInt();
playersArray[i] = new Player(score, name);
}
// sort
Arrays.sort(playersArray);
// output all
System.out.println("SCORES:");
for (final Player player : playersArray) {
System.out.println("\t" + player.name + "\t" + player.score);
}
}
}
}
类播放器实现了可比较的{
公开最终整数分数;//玩家分数
公共最终字符串名称;//玩家名称
公共播放器(最终整数分数,最终字符串名称){
这个分数=分数;
this.name=名称;
}
@覆盖公共整数比较(最终玩家其他){
返回other.score-this.score;
//返回this.score-other.score;//或按相反顺序返回
}
}
公营体育{
公共静态void main(最终字符串[]args){
try(最终扫描仪输入=新扫描仪(System.in)//
最终扫描仪输入1=新扫描仪(System.in);){
系统输出打印(“请输入玩家数量”);
final int allPlayers=input.nextInt();
最终玩家[]玩家数组=新玩家[所有玩家];
for(int i=0;i
代码中的问题是对compareTo()
方法的误解
如果第一个参数(在本例中,this
被视为第一个参数)更大,则该方法返回-1;如果它们相等,则返回0;如果第二个参数更大,则返回1
public int compareTo(Player other){
if (other == null){
return -1; // If the other is null, it goes last
}
// First, compare by the score:
int scoreComparison = Integer.compare(this.score, other.score);
// That's good enough, unless the scores are equal
if (scoreComparison != 0){ // then they're not equal, so we know our answer
return scoreComparison;
} else { // The scores are equal, so compare the names and return the result
if (this.name == null){ // Equal if both null, otherwise non-null wins
return other.name == null ? 0 : 1;
} else {
return this.name.compareTo(other.name);
}
}
}
看一看你可能的副本,先生,你是对的。我们还可以通过匿名比较器进行比较,并对compareTo()进行空检查方法。哇……真的就这些吗?为什么我想得太多了?非常感谢!@hagrawal,真的,不能输入空指针异常吗?我花了一分钟才弄清楚NPE是什么:P。