描述:java中ArrayLis的使用实际上就对数组应用的一个升级版,主要帮开发解决了数组大小和线程安全的问题。这个问题其实就是一个数据的冒泡排序来解决的问题,只是将数组改为了集合,条件改为了两个。
具体实现如下:
第一步骤:创建一个java 的工程,参考:网页链接
或者百度搜索:servlet类如何映射到url路径 百度经验
第二步骤:按要求编写学生实体类,属性:姓名,学号,课程名,成绩
如题代码如下所示:
package com.baidu.bean;
public class Students {
//名字
private String name;
//学号
private int number;
//课程名
private String courseName;
//成绩,分数
private int Achievements;
public Students(String name,int number,String courseName,int Achievements){
this.name = name;
this.number = number;
this.courseName = courseName;
this.Achievements = Achievements;
}
public Students(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getAchievements() {
return Achievements;
}
public void setAchievements(int achievements) {
Achievements = achievements;
}
}
第三步骤:创建测试类并测试。
3.1 编辑测试数据
List<Students> as = new ArrayList<Students>();
Students st1 = new Students("小明",2001,"Java",88);
Students st2 = new Students("小刚",2002,"Java",78);
Students st3 = new Students("小丁",2003,"Java",56);
Students st4 = new Students("小宏",2004,"Java",85);
Students st5 = new Students("小明",2001,"Python",84);
Students st6 = new Students("小刚",2002,"Python",98);
Students st7 = new Students("小丁",2003,"javaWeb",66);
Students st8 = new Students("小宏",2004,"Algorithm",87);
as.add(st1);
as.add(st2);
as.add(st3);
as.add(st4);
as.add(st5);
as.add(st6);
as.add(st7);
as.add(st8);
3.2 按照题目说明先将同一个学号的数据求平均值,并去除重复数据。
//求平均成绩,去除重复数据
for (int i = 0; i < as.size(); i++) {
int flag = 1;
int achievements = 0;
for (int j = i+1; j < as.size(); j++) {
if(as.get(i).getNumber() == as.get(j).getNumber()){
flag++;
achievements +=as.get(j).getAchievements();
as.remove(j);
}
}
as.get(i).setAchievements((achievements+as.get(i).getAchievements()) /flag);
}
3.3 使用冒泡排序并数据最终数据,代码如下所示
// 使用冒泡排序
for (int i = 0; i < as.size() - 1; i++) {
for (int j = 0; j < as.size() - 1; j++) {
//对象是可变的不能直接使用=,复制的是地址
Students stj = new Students(as.get(j).getName(),as.get(j).getNumber(),as.get(j).getCourseName(),as.get(j).getAchievements());
if (as.get(j+1).getAchievements() > as.get(j).getAchievements()
|| (as.get(j+1).getAchievements() == as.get(j).getAchievements() && as.get(j+1).getNumber() < as.get(j).getNumber())) {
//交互数据
as.get(j).setName(as.get(j+1).getName());
as.get(j).setNumber(as.get(j+1).getNumber());
as.get(j).setCourseName(as.get(j+1).getCourseName());
as.get(j).setAchievements(as.get(j+1).getAchievements());
as.get(j+1).setName(stj.getName());
as.get(j+1).setNumber(stj.getNumber());
as.get(j+1).setCourseName(stj.getCourseName());
as.get(j+1).setAchievements(stj.getAchievements());
}
}
}
for (int i = 0; i < as.size(); i++) {
System.out.println("name:"+as.get(i).getName()+" Name:"+as.get(i).getName()+" CourseName:"+as.get(i).getCourseName()+" Achievements:"+as.get(i).getAchievements());
}
第四步骤:最终整体代码,以及输出结果
package com.baidu.test;
import java.util.ArrayList;
import java.util.List;
import com.baidu.bean.Students;
public class TestArray {
public static void main(String[] args) {
List<Students> as = new ArrayList<Students>();
Students st1 = new Students("小明", 2001, "Java", 88);
Students st2 = new Students("小刚", 2002, "Java", 78);
Students st3 = new Students("小丁", 2003, "Java", 56);
Students st4 = new Students("小宏", 2004, "Java", 85);
Students st5 = new Students("小明", 2001, "Python", 84);
Students st6 = new Students("小刚", 2002, "Python", 98);
Students st7 = new Students("小丁", 2003, "javaWeb", 66);
Students st8 = new Students("小宏", 2004, "Algorithm", 87);
as.add(st1);
as.add(st2);
as.add(st3);
as.add(st4);
as.add(st5);
as.add(st6);
as.add(st7);
as.add(st8);
//求平均成绩,去除重复数据
for (int i = 0; i < as.size(); i++) {
int flag = 1;
int achievements = 0;
for (int j = i+1; j < as.size(); j++) {
if(as.get(i).getNumber() == as.get(j).getNumber()){
flag++;
achievements +=as.get(j).getAchievements();
as.remove(j);
}
}
as.get(i).setAchievements((achievements+as.get(i).getAchievements()) /flag);
}
// 使用冒泡排序
for (int i = 0; i < as.size() - 1; i++) {
for (int j = 0; j < as.size() - 1; j++) {
//对象是可变的不能直接使用=,复制的是地址
Students stj = new Students(as.get(j).getName(),as.get(j).getNumber(),as.get(j).getCourseName(),as.get(j).getAchievements());
if (as.get(j+1).getAchievements() > as.get(j).getAchievements()
|| (as.get(j+1).getAchievements() == as.get(j).getAchievements() && as.get(j+1).getNumber() < as.get(j).getNumber())) {
//交互数据
as.get(j).setName(as.get(j+1).getName());
as.get(j).setNumber(as.get(j+1).getNumber());
as.get(j).setCourseName(as.get(j+1).getCourseName());
as.get(j).setAchievements(as.get(j+1).getAchievements());
as.get(j+1).setName(stj.getName());
as.get(j+1).setNumber(stj.getNumber());
as.get(j+1).setCourseName(stj.getCourseName());
as.get(j+1).setAchievements(stj.getAchievements());
}
}
}
for (int i = 0; i < as.size(); i++) {
System.out.println("name:"+as.get(i).getName()+" Name:"+as.get(i).getName()+" CourseName:"+as.get(i).getCourseName()+" Achievements:"+as.get(i).getAchievements());
}
}
}
注意:这个题目是对java集合,基本对象和排序算法的一个综合应用。