【问题标题】:Java associating two ArrayLists and printing out the resultsJava 关联两个 ArrayList 并打印出结果
【发布时间】: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 分配每个学生?

标签: java arraylist nested


【解决方案1】:

如果您正在寻找学生的详细信息,您可能应该重新设计您的课程。 学生应注册一个模块。因此,学生应该有模块。并使用 Map 根据 id 检索学生。

class Student {
    String id;
    List<Module> modules = new ArrayList<Module>();
}

loadStudents() 将保持不变,只是您必须将学生 ID 的 Map 填充到您在循环中创建的 Student 对象。 loadModules() 然后会根据学生 id 从地图中挑选学生并更新模块。

这就是您需要基于 Student 的数据的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多