曾在c#实现的集合类排序问题,其中涉及泛型类的继承,方法的拓展,那么在java中该如何实现呢?其实我们说c#和java很多基础知识是相通的,换汤不换药。为此我在java中实现了类似相关功能,分享交流一下
- package 集合类测试;
- import java.util.*;
- /**
- *
- * @author Any
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Student []stu={ new Student("黑马--冯华亮",10),new Student("黑马-周鹏飞",27),new Student("黑马-何完县",21) };
- ArrayList<Student> list=new ArrayList<Student>();
- for(int i=0;i<stu.length;i++)
- {
- list.add(stu[i]);
- }
- Collections.sort(list);
- int number=list.size();
- for(int i=0;i<number;i++)
- {
- Student stu1=list.get(i);
- System.out.println("该组学生的信息如下:\n"+stu1.getName()+"-----"+stu1.getID());
- }
- }
- }
- class Student implements Comparable<Student> {
- private String name;
- private int ID;
- public Student() {
- }
- public Student(String name, int ID) {
- this.name = name;
- this.ID = ID;
- }
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the ID
- */
- public int getID() {
- return ID;
- }
- /**
- * @param ID the ID to set
- */
- public void setID(int ID) {
- this.ID = ID;
- }
- public int compareTo(Student stu) {
- //throw new UnsupportedOperationException("Not supported yet.");
- if(ID>stu.ID) return 1;
- if(ID==stu.ID) return 0;
- else return -1;
- }
-
- }
复制代码 一下,望诸君提出宝贵意见! |