java的2个ArrayList怎么比较啊~~~~帮帮忙,拜托了

fanning1989 2011-05-18 12:08:10
学生分数是在一个Arraylist,录取分数线在另一个arraylist里,怎么比较 然后输出上了录取分数线的学生信息??
...全文
338 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bawgiitx 2011-05-20
  • 打赏
  • 举报
回复

/// 人数多时,最好做成Map<分数,List<学生>>
/// 因为分数才几百个,同一个分数的人可以就有几千个
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
*
* @author ba
*/
public class px {

public static void main(String[] args) {
List<School> schools = new ArrayList();
List<Student> students = new ArrayList();

//填装数据
load(schools, students);

//学校,学生排序
java.util.Collections.sort(students);
java.util.Collections.sort(schools);

//对比
Comparator(schools, students);

//显示结果
printStudet(students);
printSchool(schools);
}

static void load(List<School> schools, List<Student> students) {
schools.add(new School("技校", 700));
schools.add(new School("新东方", 780));
schools.add(new School("清华", 500));
schools.add(new School("北大", 350));
schools.add(new School("师大", 400));
for (int i = 0; i < 10; i++) {
schools.add(new School(getNameRand() + "大学", 200 + Math.abs(rand.nextInt() % 610)));
}

for (int i = 0; i < 100; i++) {
students.add(new Student(getNameRand(), Math.abs(rand.nextInt() % 781)));
}
}

private static void Comparator(List<School> schools, List<Student> students) {
for (int i = 0, im = schools.size(); i < im; i++) {
School school = schools.get(i);
for (int j = students.size() - 1; j > -1; j--) {
Student student = students.get(j);
//上线
if (student.getScore() >= school.getScoreLine()) {
student.getSchools().add(school);
school.getStudents().add(student);
} else {
break;
}
}
}
}

private static void printStudet(List<Student> students) {
}

private static void printSchool(List<School> schools) {
if (schools != null && !schools.isEmpty()) {
for (School school : schools) {
List<Student> students = school.getStudents();
if (students == null || students.isEmpty()) {
System.out.println("" + school.getSchoolName() + " 分数线为:" + school.getScoreLine() + ",没人上线.");
} else {
System.out.println(school.getSchoolName() + " 分数线为:" + school.getScoreLine() + ",有" + students.size() + "人上线.");
System.out.println("上线名单:");
for (Student student : students) {
System.out.println(" "+student.getStudentName() + "\t" + student.getScore());
}
}
System.out.println("");
}
}
}

/**得到随机名*/
static String getNameRand() {
String n = "";
int c = rand.nextInt(10) + 1;
while (c-- > 0) {
n += getCharRand();
}
return n;
}

/**得到随机汉字*/
static char getCharRand() {
return (char) (19968 + rand.nextInt(40870 - 19968));
}
/**随机种子*/
static Random rand = new Random();
}

/**学校信息*/
class School implements Comparable<School> {

/**学校名*/
private String schoolName;
/**分数线*/
private Double scoreLine = 0.0;
/**上线的学生*/
private List<Student> students = new ArrayList();

public School() {
}

public School(String name, double sc) {
schoolName = name;
scoreLine = sc;
}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
this.students = students;
}

public String getSchoolName() {
return schoolName;
}

public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}

public Double getScoreLine() {
return scoreLine;
}

public void setScoreLine(Double scoreLine) {
this.scoreLine = scoreLine;
}

public int compareTo(School o) {
return (int) (this.scoreLine - o.scoreLine);
}
}

/**学生信息*/
class Student implements Comparable<Student> {

/**学生名*/
private String studentName;
/**分数*/
private Double score = 0.0;
/**上线的学校*/
private List<School> schools = new ArrayList();

public Student() {
}

public Student(String name, double sc) {
studentName = name;
score = sc;
}

public List<School> getSchools() {
return schools;
}

public void setSchools(List<School> schools) {
this.schools = schools;
}

public Double getScore() {
return score;
}

public void setScore(Double score) {
this.score = score;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public int compareTo(Student o) {
return (int) (this.score - o.score);
}
}

qq443507232 2011-05-19
  • 打赏
  • 举报
回复
根据业务需求 循环解决
铁匠梁 2011-05-18
  • 打赏
  • 举报
回复
1.先把学校录取分数排序
2.按学生成绩归档到哪个学校
CSDNyongwei 2011-05-18
  • 打赏
  • 举报
回复
User users=new User();
List<Integer> studenSore=new ArrayList<Integer>();
List(integer> shoolsore=new ArrayList<integer>();

for(integer shool:shoolsore){

for{Integer studen:studenSore){
if(shool<studen){
syso(users);

}

}




}

}
Intboy 版主 2011-05-18
  • 打赏
  • 举报
回复
拿一个学生成绩,去和录取成绩一个一个比较,如果大于了就退出来,如果比较完了都不大于的话就没被录取
fanning1989 2011-05-18
  • 打赏
  • 举报
回复
对,是N个学校的分数线。
wl_ldy 2011-05-18
  • 打赏
  • 举报
回复
将List 转换为Map,再进行比较会好些吧。。。
bawgiitx 2011-05-18
  • 打赏
  • 举报
回复
录取分数线,是N个学校的分数线??

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧