您的位置:首页 > 移动开发 > Objective-C

由多个Object(以Teacher为例)对象所形成的数组可以使用Arrays.sort方法进行排序(编号由低到高排序)。

2012-11-28 21:01 1006 查看
package com.testthree.third;

import java.util.Arrays;

public class Teacher implements Comparable<Object>{

/**

* @param args

*/

private int no ;

private String name;

private int age;

private String seminary;

public Teacher(int no,String name,int age,String seminary) {

this.no = no;

this.name = name;

this.age = age;

this.seminary = seminary;

}

/**

* @return the no

*/

public int getNo() {

return no;

}

/**

* @param no the no to set

*/

public void setNo(int no) {

this.no = no;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the age

*/

public int getAge() {

return age;

}

/**

* @param age the age to set

*/

public void setAge(int age) {

this.age = age;

}

/**

* @return the seminary

*/

public String getSeminary() {

return seminary;

}

/**

* @param seminary the seminary to set

*/

public void setSeminary(String seminary) {

this.seminary = seminary;

}

//實現Comparable接口中的比較方法compareTo

@Override

public int compareTo(Object o) {

// TODO Auto-generated method stub

if(this.no < ((Teacher)o).no){

return -1;

}

else if(this.no == ((Teacher)o).no)

return 0;

else

return 1;

}

public String toString() {

String Str = "编号为:" + no + "、姓名为:" + name + "、年龄为:" + age + "的"

+ seminary + "学院老师";

return Str;

}// toString

public static void main(String[] args) {

// TODO Auto-generated method stub

Teacher [] teacherArray = new Teacher[3];

teacherArray[0] = new Teacher(101,"misszhang",30,"xinxi");

teacherArray[1] = new Teacher(102,"mrzhang",24,"xindian");

teacherArray[2] = new Teacher(100,"mrli",40,"xinxi");

System.out.println("输出排序前的teacherArrray数组中的元素:");

for(int i=0 ; i<teacherArray.length ;i++)

{

System.out.println(teacherArray[i].toString());

}

Arrays.sort(teacherArray, 0, 3);

System.out.println("输出排序后的teacherArrray数组中的元素:");

for(int i=0 ; i<teacherArray.length ;i++)

{

System.out.println(teacherArray[i].toString());

}

}//main

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐