import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**
* 主类
*/
public class StudentDemo {
public static void main(String[] a) {
Student[] ss = new Student[] { new Student(10001, "翁少1", 19),
new Student(10005, "翁少2", 23), new Student(10003, "翁少3", 22),
new Student(10004, "翁少4", 23), new Student(10002, "翁少5", 22),
new Student(10006, "翁少5", 23), };
myComparable mycomp=new myComparable();
//数组排序
Arrays.sort(ss,mycomp);
//初始状态
System.out.println("初始状态:");
//输出数组
for (Student s : ss) {
System.out.println(s);
}
//给所有学生年龄加1;
for (Student s : ss) {
s.setAge(s.getAge() + 1);
}
System.out.println("给所有学生年龄加1:");
//输出数组
for (Student s : ss) {
System.out.println(s);
}
//大于20岁的学生人数
List
for (Student s : ss) {
//如果大于20岁,list增加其
if(s.getAge()>20){
list.add(s);
}
}
System.out.println("大于20岁的学生人数:"+list.size());
for (Student s : list) {
System.out.println(s);
}
}
}
/**
* 学生类
*/
class Student {
private int id;// 学号
private String name;// 姓名
private int age;// 年龄
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
//重载toString()
public String toString() {
return "学号:" + this.id + "姓名:" + this.name + "年龄:" + this.age;
}
}
//自定义的排序类。实现Comparator接口
class myComparable implements Comparator {
public int compare(Object a, Object b) {
Student s1 = (Student) a;
Student s2 = (Student) b;
int i=s1.getId() - s2.getId();
if(i>0) return 1;
else if(i<0) return -1;
else return 0;
}
}
不了解你想要怎样实现,估计你是想要进控制台输入每个学生的信息!
那很简单,你只要把初始化的数据用io方式得到就可以。
另外想问下 你确定你看明白了我写的程序 反正我感觉你是不明白