黑马程序员:学生类按成绩排序问题
2012-06-03 07:30
323 查看
--------------------android培训、java培训、期待与您交流! ------------------
现在才发现我用集合类实现的做法过于繁杂。看到黑马论坛里的同学贴出的代码,感觉有点自惭形秽,其实这个问题非常简单,只是自己把问题想得复杂了,看到这么简洁高效的实现,再对比我写的代码。呵呵,既是惊讶又是惊喜!直接在构造函数里初始化对象,比起我在集合里建立对象,省事多了。同时代码清晰,有层次感,还便于后期的维护。把代码贴出来学习下:
class Sort
{
public static void main(String[] args)
{
Student stu0 = new Student("stuA",24,80);
Student stu1 = new Student("stuB",24,90);
Student stu2 = new Student("stuC",24,78);
Student stu3 = new Student("stuD",24,66);
Student stu4 = new Student("stuE",24,89);
Student[] arr = {stu0,stu1,stu2,stu3,stu4};
for (int i = 0;i<arr.length;i++ )
{
for(int j = i+1;j<arr.length;j++)
{
if (arr[j].getExam()<arr[i].getExam())
{
Student temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("学生按成绩排序");
for (int i=0;i<arr.length ;i++ )
{
System.out.println("第"+(i+1)+"名:"+arr[i].getName()+" 成
绩:"+arr[i].getExam());
}
}
}
class Student
{
Student(){}
Student(String name,int age,int exam)
{
this.name=name;
this.age=age;
this.exam=exam;
}
private String name;
private int age;
private int exam;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
public void setExam(int exam)
{
this.exam = exam;
}
public int getExam()
{
return exam;
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima
现在才发现我用集合类实现的做法过于繁杂。看到黑马论坛里的同学贴出的代码,感觉有点自惭形秽,其实这个问题非常简单,只是自己把问题想得复杂了,看到这么简洁高效的实现,再对比我写的代码。呵呵,既是惊讶又是惊喜!直接在构造函数里初始化对象,比起我在集合里建立对象,省事多了。同时代码清晰,有层次感,还便于后期的维护。把代码贴出来学习下:
class Sort
{
public static void main(String[] args)
{
Student stu0 = new Student("stuA",24,80);
Student stu1 = new Student("stuB",24,90);
Student stu2 = new Student("stuC",24,78);
Student stu3 = new Student("stuD",24,66);
Student stu4 = new Student("stuE",24,89);
Student[] arr = {stu0,stu1,stu2,stu3,stu4};
for (int i = 0;i<arr.length;i++ )
{
for(int j = i+1;j<arr.length;j++)
{
if (arr[j].getExam()<arr[i].getExam())
{
Student temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("学生按成绩排序");
for (int i=0;i<arr.length ;i++ )
{
System.out.println("第"+(i+1)+"名:"+arr[i].getName()+" 成
绩:"+arr[i].getExam());
}
}
}
class Student
{
Student(){}
Student(String name,int age,int exam)
{
this.name=name;
this.age=age;
this.exam=exam;
}
private String name;
private int age;
private int exam;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
public void setExam(int exam)
{
this.exam = exam;
}
public int getExam()
{
return exam;
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima
相关文章推荐
- 黑马程序员---------------------声明类Students,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)。
- “黑马程序员”声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)
- 学校有多个班级,有语数外多门成绩,按照成绩排序后,找出每个班级语文成绩前五名学生
- 选择法排序—————对一个班学生成绩按大小进行排序,并对应学号
- 结构体数组排序 (学生信息 按成绩排序 )
- 项目四 用类处理“学生成绩”的问题
- 黑马程序员_日记27_JavaString类型的数字排序问题
- 指定序的排序问题,记一个学生的问题
- 泛式——学生成绩排序
- 有一个班4个学生5门课成绩,要求编程实现把这四个学生的成绩按照第一门课由小到大排序(冒泡法)。
- 黑马程序员---------异或在处理排序时的小问题
- 补3:第五次实验-问题四:学生成绩
- 学生成绩排序-练习
- 第十六周实验--任务2--学生成绩排序--从已有的文件中读入学生数据并把修改后的数据写入到另一个新创建的文件中
- 用Java编程把学生的成绩从高到低排序
- 使用TreeSet集合存储学生姓名成绩,并按照总成绩从高到低排序。
- “黑马程序员”关于排序的问题
- 问题二十六: 输入3个学生3门课的成绩,计算每个学生的总分和平均分成绩。(用数组)
- 指定序的排序问题,记一个学生的问题
- 从文件读出学生信息数据,按成绩从高到底排序,并输出其中成绩次高者的所有数据。