標題:
自訂排序 (一)
[打印本頁]
作者:
tonyh
時間:
2022-11-25 17:52
標題:
自訂排序 (一)
本帖最後由 tonyh 於 2022-11-25 21:00 編輯
試定義一集合,集合中包含五個 Student 物件,再透過實作 Comparator 介面自訂比較器,分別完成「依座號遞增排序」及「依分數遞減排序」的比較方法,以 Collections 類別下的 sort() 方法搭配自訂的比較器,分別完成「依座號遞增排序」及「依分數遞減排序」之操作練習。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ch01 {
ArrayList<Student> stu=new ArrayList<Student>();
Ch01()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new MyComparator1()); //使用自定比較器來排序
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class MyComparator1 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) { //遞增的比較方法
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) { //遞減的比較方法
return o2.score-o1.score;
}
}
class Student //內部類別
{
int num, score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
黃宥華
時間:
2022-11-25 20:29
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
public class T2 {
ArrayList<Student> stu=new ArrayList<T2.Student>();
T2(){
stu.add(new Student(4,"大熊",60));
stu.add(new Student(1,"多拉A夢",90));
stu.add(new Student(3,"靜香",100));
stu.add(new Student(2,"小夫",70));
stu.add(new Student(5,"胖虎",20));
System.out.println("原始資料:");
show();
System.out.println("座號遞增:");
Collections.sort(stu,new MyComparator1());
show();
System.out.println("分數遞減:");
Collections.sort(stu,new MyComparator2());
show();
}
class MyComparator1 implements Comparator<Student>{
public int compare(Student o1,Student o2) {
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>{
public int compare(Student o1,Student o2) {
return o2.score-o1.score;
}
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class Student{
int num,score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new T2();
}
}
複製代碼
作者:
陳宥穎
時間:
2022-11-25 20:30
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ch01 {
ArrayList<Student> stu=new ArrayList<Ch01.Student>();
Ch01()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu,new com());
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new com2());
System.out.println("依分數遞減排序:");
show();
}
class Student
{
int num,sc;
String name;
Student(int nu,String na,int s)
{
num=nu;
name=na;
sc=s;
}
}
class com implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o1.num-o2.num;
}
}
class com2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o2.sc-o1.sc;
}
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).sc);
System.out.println();
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
劉愷鈞
時間:
2022-11-25 20:38
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ch02 {
public static void main(String[] args) {
new Ch02();
}
ArrayList<Student> stu=new ArrayList<Student>();
Ch02()
{
stu.add(new Student(4,"大雄",60));
stu.add(new Student(1,"小叮噹",90));
stu.add(new Student(3,"宜靜",100));
stu.add(new Student(2,"阿福",70));
stu.add(new Student(5,"技安",20));
System.out.println("原始資料:");
show();
System.out.println("依座號遞增排序:");
Collections.sort(stu,new MyComperator1());
show();
System.out.println("依分數遞減排序:");
Collections.sort(stu,new MyComperator2());
show();
}
class MyComperator1 implements Comparator<Student>{
@Override
public int compare(Student o1,Student o2)
{
return o1.num-o2.num;
}
}
class MyComperator2 implements Comparator<Student>{
@Override
public int compare(Student o1,Student o2)
{
return o2.score-o1.score;
}
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------");
for(int i=0;i<5;i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class Student
{
int num,score;
String name;
Student(int n,String m,int s)
{
num=n;
name=m;
score=s;
}
}
}
複製代碼
作者:
李宇澤
時間:
2022-11-25 20:40
本帖最後由 李宇澤 於 2022-11-25 20:45 編輯
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ch89 {
ArrayList<Student> stu=new ArrayList<Student>();
Ch89()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new MyComparator1());
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class MyComparator1 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o2.score-o1.score;
}
}
class Student
{
int num, score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new Ch89();
}
}
複製代碼
作者:
林祐霆
時間:
2022-11-25 20:42
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SUS {
ArrayList<Student> stu=new ArrayList<Student>();
SUS()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new C1());
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new C2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class C1 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o1.num-o2.num;
}
}
class C2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o2.score-o1.score;
}
}
class Student
{
int num, score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new SUS();
}
}
複製代碼
作者:
蘇韋誠
時間:
2022-11-25 20:45
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ha {
ArrayList<Student> stu=new ArrayList<ha.Student>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ha()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new MyComparator1());
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依座號遞減排序:");
show();
}
class MyComparator1 implements Comparator<Student>
{
public int compare(Student o1,Student o2)
{
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>
{
public int compare(Student o1,Student o2)
{
return o2.score-o1.score;
}
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("------------------------");
for(Student s:stu)
{
System.out.println(s.num+"\t"+s.name+"\t"+s.score);
}
System.out.println();
}
class Student
{
int num, score;
String name;
Student(int num, String n, int s)
{
this.num=num;
name=n;
score=s;
}
}
public static void main(String[] args) throws Exception {
new ha();
}
}
複製代碼
作者:
李穎俊
時間:
2022-11-25 20:58
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class P1 {
ArrayList<Student> stu=new ArrayList<Student>();
P1()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new MyComparator1());
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class MyComparator1 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o2.score-o1.score;
}
}
class Student
{
int num, score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new P1();
}
}
複製代碼
作者:
洪承廷
時間:
2022-11-25 21:03
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class A {
ArrayList<Student> stu=new ArrayList<Student>();
A()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new MyComparator1());
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class MyComparator1 implements Comparator<Student>
{
public int compare(Student o1, Student o2) {
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o2.score-o1.score;
}
}
class Student
{
int num, score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new A();
}
}
複製代碼
作者:
尤爾呈
時間:
2022-12-2 20:03
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ch01 {
ArrayList<Student> stu=new ArrayList<Student>();
Ch01()
{
stu.add(new Student(4, "大雄", 60));
stu.add(new Student(1, "小叮噹", 90));
stu.add(new Student(3, "宜靜", 100));
stu.add(new Student(2, "阿福", 70));
stu.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(stu, new MyComparator1()); //使用自定比較器來排序
System.out.println("依座號遞增排序:");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("-------------------");
for(int i=0; i<stu.size(); i++)
System.out.println(stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class MyComparator1 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o1.num-o2.num;
}
}
class MyComparator2 implements Comparator<Student>
{
@Override
public int compare(Student o1, Student o2) {
return o2.score-o1.score;
}
}
class Student
{
int num, score;
String name;
Student(int n, String m, int s)
{
num=n;
name=m;
score=s;
}
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2