http://www.verejava.com/?id=169930999133100
/**
知识点: 比较类 Comparable
题目: 将某班学生按数学成绩从小到大排序
思路:
1. 抽象出类:
1.1 班级(ClassSet)
1.2 学生(Student)
2. 找出类关系:
2.1 学生 属于 班级 Student -> ClassSet(多对1)
3. 找出类属性:
3.1 ClassSet(班级名称,班级人数)
3.2 Student(学生名称,数学成绩)
4. 找出类方法:
4.1 学生添加到班级 ClassSet{addStudent(Student s)}
4.2 学生成绩从小到大排序 ClassSet{sortByScore()}
*/
import java.util.Arrays;
public class TestComparable
{
public static void main(String[] args)
{
//实例化4G班级
ClassSet c=new ClassSet("4G",4);
//添加学生
c.addStudent(new Student("李明",90));
c.addStudent(new Student("李浩",80));
c.addStudent(new Student("王涛",95));
c.addStudent(new Student("张胜",70));
//获得4G班级学生数组集合
Student[] students=c.getStudents();
//输出学生信息
for(Student s:students)
{
if(s!=null)
System.out.println(s.getName()+","+s.getMathScore());
}
System.out.println("\n根据学生成绩排序");
//c.sortByBuble();
//Arrays.sort(students);
c.sortByMerge();
for(Student s:students)
{
if(s!=null)
System.out.println(s.getName()+","+s.getMathScore());
}
}
}
class ClassSet
{
private String className;//班级名称
private int maxSize;//班级学生人数
private int currentSize;//当前多少学生
private Student[] students;//所有学生的数组
public ClassSet(String className,int maxSize)
{
this.className=className;
this.maxSize=maxSize;
students=new Student[maxSize];
}
public Student[] getStudents()
{
return this.students;
}
/**
添加学生
*/
public void addStudent(Student s)
{
for(int i=0;i<students.length;i++)
{
if(students[i]==null)
{
students[i]=s;
currentSize++;
break;
}
}
}
/**
按照学生的数学成绩冒泡排序从小到大排序
*/
public Student[] sortByBuble()
{
for(int i=0;i<currentSize-1;i++)
{
for(int j=0;j<currentSize-i-1;j++)
{
Student s=students[j];
if(students[j].getMathScore()>students[j+1].getMathScore())
{
students[j]=students[j+1];
students[j+1]=s;
}
}
}
return this.students;
}
public void sortByMerge()
{
//创建一个临时数组存放分区元素
Comparable[] tempArray=students.clone();
merge_sort(students,tempArray,0,students.length);
}
private void merge_sort(Comparable[] array,Comparable[] tempArray, int first, int last) {
if(first+1<last)
{
int mid=(first+last)/2;
// 对左半部份排序
merge_sort(array,tempArray,first,mid);
// 对有半部分排序
merge_sort(array,tempArray,mid,last);
// 合并到一个临时数组,再拷贝到array中
merge(array,tempArray,first,mid,last);
}
}
private void merge(Comparable[] array,Comparable[] tempArray, int first, int mid,int last) {
int beginLeft=first;
int beginRight=mid;
int k=first;
while((beginLeft<mid)&&(beginRight<last)){
if(array[beginLeft].compareTo(array[beginRight])<0){
tempArray[k]=array[beginLeft];
beginLeft++;
}else{
tempArray[k]=array[beginRight];
beginRight++;
}
k++;
}
while(beginLeft<mid){
tempArray[k++]=array[beginLeft++];
}
while(beginRight<last){
tempArray[k++]=array[beginRight++];
}
for(int i=first;i<last;i++){
array[i]=tempArray[i];
}
}
}
class Student implements Comparable
{
private String name;//学生姓名
private int mathScore;//数学成绩
public Student(String name,int mathScore)
{
this.name=name;
this.mathScore=mathScore;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
public int getMathScore()
{
return this.mathScore;
}
public void setMathScore(int mathScore)
{
this.mathScore=mathScore;
}
/**
实现Comparable 接口要复写 compareTo(T o) 方法
如果从小到大排序
大于 则返回 1
小于 则返回 -1
等于 则返回 0
如果从大到小排序
大于 则返回 -1
小于 则返回 1
等于 则返回 0
*/
public int compareTo(Object obj)
{
if(obj instanceof Student)
{
Student s=(Student)obj;
if(this.mathScore>s.getMathScore())
return 1;
if(this.mathScore<s.getMathScore())
return -1;
}
return 0;
}
}
http://www.verejava.com/?id=169930999133100
原文地址:https://www.cnblogs.com/verejava/p/9216575.html
时间: 2024-11-07 11:35:10