成绩属性类
public class ChengJi {
int yuwen;
int shuxue;
int yingyu;
int zonghe;
int id;
public ChengJi(int yuwen, int shuxue, int yingyu, int id) {
this.id = id;
this.yuwen = yuwen;
this.shuxue = shuxue;
this.yingyu = yingyu;
this.zonghe = yuwen+shuxue+yingyu;
}
}
List<T>泛型集合中对象的属性冒泡排序时需要用set方法进行交换值。
成绩测试类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class ChengJiTest {
public static void main(String[] args) {
List<ChengJi> chengJiList = new ArrayList<ChengJi>();
ChengJi chengJi1 = new ChengJi(56, 78, 100, 1);
ChengJi chengJi2 = new ChengJi(45, 100, 90, 2);
ChengJi chengJi3 = new ChengJi(100, 89, 46, 3);
ChengJi chengJi4 = new ChengJi(25, 100, 78, 4);
ChengJi chengJi5 = new ChengJi(34, 45, 90, 5);
ChengJi chengJi6 = new ChengJi(89, 56, 100, 6);
ChengJi chengJi7 = new ChengJi(100, 100, 100, 7);
chengJiList.add(chengJi1);
chengJiList.add(chengJi2);
chengJiList.add(chengJi3);
chengJiList.add(chengJi4);
chengJiList.add(chengJi5);
chengJiList.add(chengJi6);
chengJiList.add(chengJi7);
for (int i = 0; i < chengJiList.size() - 1; i++) {
for (int j = 0; j < chengJiList.size() - i - 1; j++) {
ChengJi temp;
if (chengJiList.get(j).zonghe < chengJiList.get(j + 1).zonghe) {
temp = chengJiList.get(j);
chengJiList.set(j,chengJiList.get(j + 1));
chengJiList.set((j+1),temp);
}else if(chengJiList.get(j).zonghe == chengJiList.get(j + 1).zonghe){
// 总成绩相等时,数学成绩高的排在前面
if(chengJiList.get(j).shuxu < chengJiList.get(j + 1).shuxu){
temp = chengJiList.get(j);
chengJiList.set(j,chengJiList.get(j + 1));
chengJiList.set((j+1),temp);
}
}
}
}
System.out.println(chengJiList);
}
}