(java实现)
题目描述:
查找和排序
题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。
示例:
jack 70
peter 96
Tom 70
smith 67
从高到低 成绩
peter 96
jack 70
Tom 70
smith 67
从低到高
smith 67
jack 70
Tom 70
peter 96
输入描述:
注意一个case里面有多组样例,请用循环处理输入 输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开。
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
示例1:
输入
3 0 fang 90 yang 50 ning 70
输出
fang 90 ning 70 yang 50
问题分析:
略
相关知识:
Collection.sort(list,new Comparator<class>()
{
public int compare(class c1, class c2)
{
return c1.attribute-c2.attribute;
}
});参考代码:
思路一实现:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while (input.hasNext())
{
int num = input.nextInt();
int flag = input.nextInt();
List<Student> stuList = new ArrayList<Student>();
for (int i=0; i<num; i++)
{
stuList.add(new Student(input.next(),input.nextInt()));
}
if (0 == flag)
{
Collections.sort(stuList, new gradeDESCComparator());
/*
Collections.sort(stuList,new Comparator<Student>()
{
public int compare(Student s1,Student s2)
{
return s2.score-s1.score; //降序
}
});*/
}else if (1 == flag)
{
Collections.sort(stuList, new gradeASCComparator());
/*
Collections.sort(stuList,new Comparator<Student>()
{
public int compare(Student s1,Student s2)
{
return s1.score-s2.score; //升序
}
});*/
}
for (int i=0; i<stuList.size(); i++)
{
System.out.println(stuList.get(i).name+" "+stuList.get(i).score);
}
}
}
}
class Student
{
String name;
int score;
public Student(String name, int score)
{
this.name = name;
this.score = score;
}
}
class gradeDESCComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s2.score-s1.score; //降序
}
}
class gradeASCComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s1.score-s2.score; //升序
}
}相关知识
Java中Comparator的使用
Comparable与Comparator的区别
Comparable & Comparator都是用来实现集合中元素的比较、排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以,如想实现排序,就需要在集合外定义Comparator接口的方法或在集合内实现 Comparable接口的方法,Comparator位于包java.util下,而Comparable位于包java.lang下。
Java中有两种方式来提供比较功能。第一种是实现java.lang.Comparable接口,使你的类天生具有比较的能力,此接口很简单,只有一个compareTo一个方法。此方法接收另一个Object为参数,如果当前对象小于参数则返回负值,如果相等则返回零,否则返回正值,也就是:
x.compareTo(y) 来“比较x和y的大小”。若返回“负数”,意味着“x比y小”;返回“零”,意味着“x等于y”;返回“正数”,意味着“x大于y”。
使用Comparable比较的例子:
class Person implements Comparable<Person>{
@Override
public int compareTo(Person person) {
return name.compareTo(person.name);
//return this.name - person.name;
}
}
ArrayList<Person> list = new ArrayList<Person>();
// 添加对象到ArrayList中
list.add(new Person("aaa", 10));
list.add(new Person("bbb", 20));
list.add(new Person("ccc", 30));
list.add(new Person("ddd", 40));
Collections.sort(list); //这里会自动调用Person中重写的compareTo方法。使用Comparator比较的例子:
public class ComparatorDemo {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Joe", 24),
new Person("Pete", 18),
new Person("Chris", 21)
);
Collections.sort(people, new LexicographicComparator());
System.out.println(people);
//[{name=Chris, age=21}, {name=Joe, age=24}, {name=Pete, age=18}]
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person a, Person b) {
// TODO Auto-generated method stub
return a.age < b.age ? -1 : a.age == b.age ? 0 : 1;
}
});
System.out.println(people);
//[{name=Pete, age=18}, {name=Chris, age=21}, {name=Joe, age=24}]
}
}
class LexicographicComparator implements Comparator<Person> {
@Override
public int compare(Person a, Person b) {
return a.name.compareToIgnoreCase(b.name);
}
}
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
@Override
public String toString() {
return String.format("{name=%s, age=%d}", name, age);
}
}可以通过两种方式去实现自定义排序:
第一种:
定义一个类去实现Comparator接口,重写其中的compare方法。
第二种:
其实只是语法不同,在内部就new这个接口并重写里面的compare方法。

京公网安备 11010502036488号