Java实战-对象数组排序实例
package com.mldn;
import java.util.Arrays;
class Student implements Comparable
{
private String name;
private int age;
private float score;
private int number; // 学生编号
public Student()
{
this.name = null;
this.age = 0;
this.score = 0.0f;
this.number = 0;
}
public Student(int number, String name, int age, float score)
{
this.number = number;
this.name = name;
this.age = age;
this.score = score;
}
public int compareTo(Student stu) // 实现Comparable接口
{
if (this.score > stu.score)
{
return 1;
}
else if (this.score == stu.score)
{
return 0;
}
else
{
return -1;
}
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public float getScore()
{
return this.score;
}
public int getNumber()
{
return this.number;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void setScore(float score)
{
this.score = score;
}
public void setNumber(int number)
{
this.number = number;
}
public String toString()
{
return “学生编号:” + this.getNumber() + ” 姓名:” + this.getName() + ” 年龄: ” + this.getAge() + ” 成绩:” + this.getScore();
}
}
public class ScoreList
{
public static void main(String[] args)
{
Student[] stu = new Student[5];
stu[0] = new Student(1, “zhansan”, 20, 80.0f);
stu[1] = new Student(2, “lisi”, 21, 89.5f);
stu[2] = new Student(3, “abcd”, 23, 90.0f);
stu[3] = new Student(4, “zhdfn”, 22, 87.0f);
stu[4] = new Student(5, “zdsf”, 24, 79.5f);
System.out.println(“排序之前:”);
for (int x = 0; x < stu.length; x++)
{
System.out.println(stu[x]);
}
System.out.println(“排序之后:”);
Arrays.sort(stu);
for (int x = 0; x < stu.length; x++)
{
System.out.println(stu[x]);
}
}
}
/*
administrator@xu-desktop:~$ javac -d . work/ScoreList.java
administrator@xu-desktop:~$ java com.mldn.ScoreList
排序之前:
学生编号:1 姓名:zhansan 年龄: 20 成绩:80.0
学生编号:2 姓名:lisi 年龄: 21 成绩:89.5
学生编号:3 姓名:abcd 年龄: 23 成绩:90.0
学生编号:4 姓名:zhdfn 年龄: 22 成绩:87.0
学生编号:5 姓名:zdsf 年龄: 24 成绩:79.5
排序之后:
Exception in thread “main” java.lang.ClassCastException: com.mldn.Student cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at com.mldn.ScoreList.main(ScoreList.java:93)
—————– 实现Comparable接口后:
administrator@xu-desktop:~$ javac -d . work/ScoreList.java
administrator@xu-desktop:~$ java com.mldn.ScoreList
排序之前:
学生编号:1 姓名:zhansan 年龄: 20 成绩:80.0
学生编号:2 姓名:lisi 年龄: 21 成绩:89.5
学生编号:3 姓名:abcd 年龄: 23 成绩:90.0
学生编号:4 姓名:zhdfn 年龄: 22 成绩:87.0
学生编号:5 姓名:zdsf 年龄: 24 成绩:79.5
排序之后:
学生编号:5 姓名:zdsf 年龄: 24 成绩:79.5
学生编号:1 姓名:zhansan 年龄: 20 成绩:80.0
学生编号:4 姓名:zhdfn 年龄: 22 成绩:87.0
学生编号:2 姓名:lisi 年龄: 21 成绩:89.5
学生编号:3 姓名:abcd 年龄: 23 成绩:90.0
*/
——————二叉树排序原理: 中序遍历,使用Comparable接口实现对象排序
package com.mldn;
class BinaryTree // 二叉树操作类,
{
class Node
{
private Comparable data; // 保存所有实现了Comparable接口的类型数据;
private Node left; // 左子树
private Node right; // 右子树
public Node(Comparable data)
{
this.data = data; // 构造
}
public void addNode(Node newNode)
{
if (newNode.data.compareTo(this.data) <= 0)
{
if (this.left == null)
{
this.left = newNode; // 保存在左子树
}
else
{
this.left.addNode(newNode); // 继续往下添加
}
}
else
{
if (this.right == null)
{
this.right = newNode; // 保存在右子树
}
else
{
this.right.addNode(newNode); // 继续往下添加
}
}
}
public void printNode() // 采用中序遍历
{
if (this.left != null)
{
this.left.printNode(); // 继续遍历
}
System.out.println(this.data); // 打印根节点
if (this.right != null)
{
this.right.printNode(); //
}
}
}
private Node root;
public BinaryTree()
{
this.root = null; // 根节点为空
}
public void add(Comparable data)
{
Node newNode = new Node(data); // 实例化节点类
if (this.root == null)
{
this.root = newNode; // 设为根节点
}
else
{
this.root.addNode(newNode); // 添加新节点
}
}
public void print()
{
this.root.printNode(); // 打印所有节点
}
}
public class BinaryTreeDemo
{
public static void main(String[] args)
{
BinaryTree bt = new BinaryTree();
bt.add(14);
bt.add(154);
bt.add(314);
bt.add(154);
bt.add(145);
bt.add(142);
bt.print();
}
}
/*
administrator@xu-desktop:~$ javac -d . work/BinaryTreeDemo.java
注意:work/BinaryTreeDemo.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。
administrator@xu-desktop:~$ java com.mldn.BinaryTreeDemo
14
142
145
154
154
314
*/
————– 实现Comparator接口排序:比较麻烦!
对于已经开发完成的类,不可能再去修改来实现Comparable接口,这时可以使用Comparator接口完成排序;
Arrays类中提供了如下排序算法:
public static
java.util.Comparator:
public interface Comparator
{
int compare(T o1, T o2);
boolean equals(Object obj); // equals方法是每个类本应该覆写的方法,他来自于Object!
}
—————–实例:
–1.实现一个不具有排序功能的标准类: Student,默认实现了equals方法
package com.mldn;
class Student
{
private String name;
private int age;
private float score;
private int number; // 学生编号
public Student()
{
this.name = null;
this.age = 0;
this.score = 0.0f;
this.number = 0;
}
public Student(int number, String name, int age, float score)
{
this.number = number;
this.name = name;
this.age = age;
this.score = score;
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public float getScore()
{
return this.score;
}
public int getNumber()
{
return this.number;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void setScore(float score)
{
this.score = score;
}
public void setNumber(int number)
{
this.number = number;
}
public boolean equals(Object obj) // 覆写equals
{
if (this == obj)
{
return true;
}
if (!(obj instanceof Student))
{
return false;
}
Student stu = (Student)obj; // 向下转型
if (stu.number == this.number && stu.name.equals(this.name) && stu.age == this.age && stu.score == this.score)
{
return true;
}
return false;
}
public String toString() // 覆写toString
{
return “学生编号:” + this.getNumber() + ” 姓名:” + this.getName() + ” 年龄: ” + this.getAge() + ” 成绩:” + this.getScore();
}
}
—-2.再针对Student类创建实现Comparator接口的类
package com.mldn;
import java.util.Comparator; // 载入接口
class StudentCompare implements Comparator
{
public int compare(Student stu1, Student stu2)
{
if (stu1.getScore() > stu2.getScore())
{
return 1;
}
else if (stu1.getScore() == stu2.getScore())
{
if (stu1.getAge() > stu2.getAge())
{
return 1;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
public boolean equals(Object obj)
{
return this.equals(obj);
}
}
—-3.最后测试排序算法:
package com.mldn;
import java.util.Arrays;
public class ComparatorDemo
{
public static void main(String[] args)
{
Student[] stu = new Student[5]; // 声明对象数组并开辟5空间
stu[0] = new Student(1, “zhansan”, 20, 90); // 实例化成员
stu[1] = new Student(2, “eranman”, 21, 95);
stu[2] = new Student(3, “blanlan”, 23, 90);
stu[3] = new Student(4, “agandan”, 25, 98);
stu[4] = new Student(5, “seantan”, 26, 97);
StudentCompare stuCompare = new StudentCompare(); // 针对Student类实现Comparator接口的比较器类,类似适配器!
Arrays.sort(stu, stuCompare); // 按升序排序数组
for (int x = 0; x < stu.length; x++)
{
System.out.println(stu[x]); // 打印第x个成员
}
}
}
/*
administrator@xu-desktop:~$ javac -d . work/ComparatorDemo.java
administrator@xu-desktop:~$ java com.mldn.ComparatorDemo
学生编号:1 姓名:zhansan 年龄: 20 成绩:90.0
学生编号:3 姓名:blanlan 年龄: 23 成绩:90.0
学生编号:2 姓名:eranman 年龄: 21 成绩:95.0
学生编号:5 姓名:seantan 年龄: 26 成绩:97.0
学生编号:4 姓名:agandan 年龄: 25 成绩:98.0
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《Java实战-对象数组排序实例》
本文地址:http://www.xiupu.net/archives-76.html
关注公众号:
微信赞赏
支付宝赞赏