我试图弄清楚如何使用可比较的方式对ArrayList进行排序,我的代码如下所示:
public class playerComparsion{
public static void main(String[] args){
ArrayList<Object> list = new ArrayList<Object>();
Player p1 = new Players(1,92,Zlatan);
Player p2 = new Players(2,92,Hazard);
Player p3 = new Players(1,82,Klose);
list.add(p1);
list.add(p2);
list.add(p3);
}
}
class Players implements Comparable{
int position;
String name;
int rating;
public Players(int i, int j, String string) {
this.position=i;
this.rating=j;
this.name=string;
}
public void getRating() {
System.out.println(this.rating);
}
public void getPos() {
System.out.println(this.position);
}
public void getName() {
System.out.println(this.name);
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
我想根据属性评级对Arraylist进行排序.我想我应该使用compareTo函数,但我不知道怎么会有人帮助我?
解决方法:
通过实现Comparator< Player>而不是让Player实现Comparable,您可以获得更大的灵活性.类.例如:
class PlayerComparatorByRating implements Comparator<Player> {
@Override
public int compare(Player o1, Player o2) {
return o1.getRating() - o2.getRating();
}
}
class PlayerComparatorByName implements Comparator<Player> {
@Override
public int compare(Player o1, Player o2) {
return o1.getName().compareTo(o2.getName());
}
}
毕竟,玩家有多个领域,很容易想象有时你可能想要以不同的方式订购玩家.这种方法的一大优势是单一责任原则:Player类只做一件事,封装了玩家数据.而不是再增加一个责任(排序),最好将该逻辑移到另一个类中.
您可以将这些比较器与Collections.sort一起使用,例如:
Collections.sort(list, new PlayerComparatorByRating());
System.out.println(list);
Collections.sort(list, new PlayerComparatorByName());
System.out.println(list);
额外提示
你的班级似乎被命名为玩家.最好重命名为Player.
getName,getRating,getPos方法不应返回void并打印结果,而是返回字段值.
为构造函数参数使用更好的名称,例如:
Player(int position, int rating, String name) {
this.position = position;
this.rating = rating;
this.name = name;
}
使用正确类型的列表来存储玩家:
List<Player> list = new ArrayList<Player>();
请正确格式化您的代码.任何IDE都可以做到这一点.
建议实施
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Player {
private int position;
private int rating;
private final String name;
Player(int position, int rating, String name) {
this.position = position;
this.rating = rating;
this.name = name;
}
public int getRating() {
return rating;
}
public int getPos() {
return position;
}
public String getName() {
return name;
}
@Override
public String toString() {
return String.format("%s:%d:%d", name, position, rating);
}
}
class PlayerComparatorByRating implements Comparator<Player> {
@Override
public int compare(Player o1, Player o2) {
return o1.getRating() - o2.getRating();
}
}
class PlayerComparatorByName implements Comparator<Player> {
@Override
public int compare(Player o1, Player o2) {
return o1.getName().compareTo(o2.getName());
}
}
public class PlayerComparatorDemo {
public static void main(String[] args){
List<Player> list = new ArrayList<Player>();
Player p1 = new Player(1, 92, "Zlatan");
Player p2 = new Player(2, 92, "Hazard");
Player p3 = new Player(1, 82, "Klose");
list.add(p1);
list.add(p2);
list.add(p3);
Collections.sort(list, new PlayerComparatorByRating());
System.out.println(list);
Collections.sort(list, new PlayerComparatorByName());
System.out.println(list);
}
}