【发布时间】:2014-02-09 14:15:10
【问题描述】:
我这里有一组三个 ArrayList。一个 ArrayList 包含 name 、 surname 、 uid 和 degree 计划的学生。然后我还有一个模块列表,每个模块还包含一个学生 ID 的数组列表,用于在该特定模块上注册的学生。
我要做的是将学生的数组列表与用户 ID 的数组列表链接起来,这样程序就会查看 ID 并将它们与包含所有学生详细信息的列表进行比较,然后编写一个组合报告完整的学生详细信息和他们注册的模块。
我的数组列表设置得很好,但是我很难使用 ID 访问内部数组列表。解释起来很棘手,但这里是我的 Application 类的代码,它将所有东西放在一起,你可以看到 ArrayLists 是如何布局的。
import java.util.*;
import java.io.*;
public class Model {
private ArrayList<Student> students;
private ArrayList<Module> modules;
private Module moduleLink;
public Model(){
students = new ArrayList<Student>();
modules = new ArrayList<Module>();
}
public void runTests() throws FileNotFoundException{
System.out.println("Beginning program, the ArrayList of students will now be loaded");
loadStudents("studentlist.txt");
System.out.println("Load attempted, will now print off the list");
printStudents();
System.out.println("The module list will now be loaded and printed");
loadModules("moduleslist.txt");
printModules();
System.out.println("Modules printed, ArrayList assosciation will commence");
}
public void printStudents(){
for(Student s: students){
System.out.println(s.toString());
}
}
public void printModules(){
for(Module m: modules){
System.out.println(m.toString());
}
}
public void loadStudents(String fileName) throws FileNotFoundException{
Scanner infile =new Scanner(new InputStreamReader
(new FileInputStream(fileName)));
int num=infile.nextInt();infile.nextLine();
for (int i=0;i<num;i++) {
String u=infile.nextLine();
String s=infile.nextLine();
String n=infile.nextLine();
String c=infile.nextLine();
Student st = new Student(u,s,n,c);
students.add(st);
}
infile.close();
}
public void loadModules(String fileName) throws FileNotFoundException{
Scanner infile =new Scanner(new InputStreamReader
(new FileInputStream(fileName)));
int numModules = infile.nextInt();
infile.nextLine();
for (int i=0;i<numModules;i++){
String code = infile.nextLine();
int numStudents = infile.nextInt();
infile.nextLine();
ArrayList<Student> enrolledStudents = new ArrayList<Student>(numStudents);
for (int a=0;a<numStudents;a++){
String uid = infile.nextLine();
Student st = new Student(uid);
enrolledStudents.add(st);
}
Module m = new Module(code,enrolledStudents );
modules.add(m);
}
infile.close();
}
}
任何帮助将不胜感激,谢谢。
【问题讨论】:
-
您面临的具体问题是什么?为什么不使用 Map
来按 ID 分配每个学生?