容器的比较

来源:互联网 发布:网络机顶盒app下载 编辑:程序博客网 时间:2024/06/07 03:47
package 第11章;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;


class Stu implements Comparable<Stu> {
private int id;


/**
* @return the id
*/
public int getId() {
return id;
}


public Stu(int id, int score, String name) {
this.id = id;
this.score = score;
this.name = name;
}


/**
* @param id
*            the id to set
*/
public void setId(int id) {
this.id = id;
}


/**
* @return the score
*/
public int getScore() {
return score;
}


/**
* @param score
*            the score to set
*/
public void setScore(int score) {
this.score = score;
}


/**
* @return the name
*/
public String getName() {
return name;
}


/**
* @param name
*            the name to set
*/
public void setName(String name) {
this.name = name;
}


private int score;
private String name;


@Override
public String toString() {
return "id=" + this.id +"  "+ "score=" + this.score + " "+"name=" + this.name;


}


@Override
public int compareTo(Stu o) {
// TODO Auto-generated method stub


return this.score - o.score;
}


}


public class DataDemo7 {
/*
* (non-Javadoc)

* @see java.lang.Object#toString()
*/


public static void main(String[] args) {
Stu[] stus = { new Stu(1, 50, "1"), new Stu(2, 20, "2"),
new Stu(3, 40, "3"), new Stu(4, 40, "4"), new Stu(5, 10, "5") };
Arrays.sort(stus);
for(int i=0;i<stus.length;i++)
  {
List<Stu> list=new ArrayList<Stu>();
            list.add(stus[i]);
System.out.println(list.toString());
  }
}
}
0 0