题目:已知User类,该类有name(String类型)和age(int类型)两个属性,请编写代码
题目:已知User类,该类有name(String类型)和age(int类型)两个属性,请编写代码实现给ArrayList<User>排序,要求按照User对象的age的...
题目:已知User类,该类有name(String类型)和age(int类型)两个属性,请编写代码实现给ArrayList<User>排序,要求按照User对象的age的倒序排序。求代码啊,谢谢
展开
2个回答
展开全部
//实现Comparator接口,以达到自定义排序的目的
public class TestSort implements Comparator<User> {
public static void main(String[] args) {
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("a", 2));
userList.add(new User("b", 1));
userList.add(new User("c", 3));
for (User u : userList) {
System.out.println(u);
}
System.out.println("-----排序后-------");
//集合排序用Collections工具类,数组用Arrays
Collections.sort(userList, new TestSort());//按照TestSort对象中的compare()方法排序
for (User u : userList) {
System.out.println(u);
}
}
//实现接口后,必须重写的排序方法
public int compare(User o1, User o2) {
return o2.getAge() - o1.getAge();//正整数,零,负整数
}
}
public class TestSort implements Comparator<User> {
public static void main(String[] args) {
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("a", 2));
userList.add(new User("b", 1));
userList.add(new User("c", 3));
for (User u : userList) {
System.out.println(u);
}
System.out.println("-----排序后-------");
//集合排序用Collections工具类,数组用Arrays
Collections.sort(userList, new TestSort());//按照TestSort对象中的compare()方法排序
for (User u : userList) {
System.out.println(u);
}
}
//实现接口后,必须重写的排序方法
public int compare(User o1, User o2) {
return o2.getAge() - o1.getAge();//正整数,零,负整数
}
}
展开全部
Test类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test implements Comparator<User> {
public static void main(String[] args) {
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("张三",25));
userList.add(new User("李四",21));
userList.add(new User("王五",23));
for (User user : userList) {
System.out.println(user.getName() + "," + user.getAge());
}
Collections.sort(userList, new Test());
for (User user : userList) {
System.out.println(user.getName() + "," + user.getAge());
}
}
public int compare(User o1, User o2) {
return o2.getAge() - o1.getAge(); // (降序)
// return o1.getAge() - o2.getAge(); // (升序)
}
}
User类:
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test implements Comparator<User> {
public static void main(String[] args) {
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("张三",25));
userList.add(new User("李四",21));
userList.add(new User("王五",23));
for (User user : userList) {
System.out.println(user.getName() + "," + user.getAge());
}
Collections.sort(userList, new Test());
for (User user : userList) {
System.out.println(user.getName() + "," + user.getAge());
}
}
public int compare(User o1, User o2) {
return o2.getAge() - o1.getAge(); // (降序)
// return o1.getAge() - o2.getAge(); // (升序)
}
}
User类:
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询