標題:
自訂排序 (一)
[打印本頁]
作者:
tonyh
時間:
2021-6-26 15:28
標題:
自訂排序 (一)
本帖最後由 tonyh 於 2021-7-3 15:23 編輯
試定義一集合,集合中包含五個 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();
}
}
複製代碼
作者:
蔡幸融
時間:
2021-7-3 15:21
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Ch01 {
ArrayList<Student> stu=new ArrayList<>();
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 MyComparator());
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 MyComparator 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;
}
}
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();
}
}
複製代碼
作者:
陳泓瑜
時間:
2021-7-3 15:22
package com.buriku.nayoni.d210703;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class D210703 {
ArrayList<STUDENT> stu = new ArrayList<STUDENT>();
public D210703() {
// TODO Auto-generated constructor stub
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 COMPARATOR_NUM());
System.out.println("依座號遞增排序");
show();
Collections.sort(stu, new COMPARATOR_SCORE());
System.out.println("依分數遞減排序");
show();
}
public 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();
}
public class COMPARATOR_NUM implements Comparator<STUDENT>{
@Override
public int compare(STUDENT arg0, STUDENT arg1) {
// TODO Auto-generated method stub
return arg0.num - arg1.num;
}
}
public class COMPARATOR_SCORE implements Comparator<STUDENT>{
@Override
public int compare(STUDENT arg0, STUDENT arg1) {
// TODO Auto-generated method stub
return arg1.score - arg0.score;
}
}
public class STUDENT { //INTERNAL CLASS
int num, score;
String name;
public STUDENT(int n, String nm, int s) {
// TODO Auto-generated constructor stub
num = n;
name = nm;
score = s;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new D210703();
}
}
複製代碼
作者:
林侑成
時間:
2021-7-3 15:25
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Stacking {
ArrayList<Student> stu=new ArrayList<>();
Stacking()
{
stu.add(new Student(4, "A", 60));
stu.add(new Student(1, "B", 90));
stu.add(new Student(3, "C", 100));
stu.add(new Student(2, "D", 70));
stu.add(new Student(5, "E", 20));
System.out.println("原始資料: ");
show();
Collections.sort(stu, new MyComparator());
System.out.println("依座號遞增排序: ");
show();
Collections.sort(stu, new MyComparator2());
System.out.println("依分數遞減排序: ");
show();
}
void show()
{
System.out.println("num\tname\tscore");
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 MyComparator 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;
}
}
class Student
{
int num, score;
String name;
Student(int num, String name,int score)
{
this.num=num;
this.name=name;
this.score=score;
}
}
public static void main(String[] args) throws IOException, Exception {
new Stacking();
}
}
複製代碼
作者:
蔡依宸
時間:
2021-7-3 15:25
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class s {
ArrayList<Student> stu=new ArrayList<Student>();
s()
{
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.print("原始資料");
show();
Collections.sort(stu, new MyC1());
System.out.println("依座號排序:");
show();
Collections.sort(stu, new MyC2());
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("\t"+stu.get(i).num+"\t"+stu.get(i).name+"\t"+stu.get(i).score);
System.out.println();
}
class MyC1 implements Comparator<Student>
{
public int compare(Student o1,Student o2) {
return o1.num-o2.num;
}
}
class MyC2 implements Comparator<Student>
{
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) {
// TODO 自動產生的方法 Stub
new s();
}
}
複製代碼
作者:
陳智鈞
時間:
2021-7-3 15:32
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>
{
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;
}
}
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();
}
}
複製代碼
作者:
蔡季庭
時間:
2021-7-3 15:35
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
public class Ch01 {
ArrayList<Student> stu=new ArrayList<>();
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 Mycomparator2());
System.out.println("依座號排序:");
show();
Collections.sort(stu,new Mycomparator1());
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 Mycomparator2 implements Comparator<Student>
{
public int compare(Student o1,Student o2) {
return o1.num-o2.num;
}
class Mycomparator1 implements Comparator<Student>
{
public int compare(Student o1,Student o2) {
return o2.num-o1.num;
}
}
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();
}
}
複製代碼
作者:
洪藜芸
時間:
2021-7-3 15:37
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
public class cccc {
ArrayList<Student> sa=new ArrayList<Student>();
cccc()
{
sa.add(new Student(4, "大雄", 60));
sa.add(new Student(1, "小叮噹", 90));
sa.add(new Student(3, "宜靜", 100));
sa.add(new Student(2, "阿福", 70));
sa.add(new Student(5, "技安", 20));
System.out.println("原始資料:");
show();
Collections.sort(sa, new C());
System.out.println("依座號遞增排序:");
show();
Collections.sort(sa, new C2());
System.out.println("依分數遞減排序:");
show();
}
void show()
{
System.out.println("座號\t姓名\t分數");
System.out.println("---------------------");
for(int i=0;i<sa.size();i++)
System.out.println(sa.get(i).num+"\t"+sa.get(i).name+"\t"+sa.get(i).score);
System.out.println();
}
class C implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s1.num-s2.num;
}
}
class C2 implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
return s2.score-s1.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) throws Exception{
new cccc();
}
}
複製代碼
作者:
戴偉宸
時間:
2021-7-10 13:47
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>
{
@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 A();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2