简体   繁体   English

使用扫描器将Java加载到ArrayList的ArrayList中

[英]Java Load in an ArrayList of ArrayList using scanner

I'm trying to use scanner to load in an ArrayList of Modules, and then each module also contains an ArrayList of Students which are enrolled for that module. 我正在尝试使用扫描仪加载模块ArrayList,然后每个模块还包含针对该模块注册的Student ArrayList。 This is my constructor for the Modules: 这是我的模块构造函数:

public Module(String newCode,  ArrayList<Student> students){


}

The data is to be loaded from a text file which is laid out like this: 数据将从文本文件加载,该文本文件的布局如下:

5 (number of modules)
CS10110 (module code)
2 (number of students enrolled in the above module)
fyb9 (student enrolled)
lfr8 (student enrolled)
CS10310 
0 
CS12130 
1 
fyb9 
CS12230 
1 
lfr8 
CS10610 
2 
fyb9 
lfr8 

I've been able to use scanner to load in students like so: 我已经能够使用扫描仪像这样加载学生:

 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();

}   

But I am struggling with loading in an ArrayList with ArrayLists inside it, this is what my current crude code looks like : 但是我正在努力加载其中包含ArrayLists的ArrayList,这就是我目前的原始代码:

public void loadModules(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 c=infile.nextLine();
        ArrayList<Student> a =infile.next();


        Module m = new Module(c,a);

But obviously this does not work. 但这显然行不通。 Any ideas or tips here would be highly appreciated 这里的任何想法或技巧将不胜感激

infile.next() returns a String value, so it cannot populate an ArrayList<Student> . infile.next()返回一个String值,因此它不能填充ArrayList<Student> A general way of populating an ArrayList<ArrayList<String>> from a text file might be as follows: 从文本文件填充ArrayList<ArrayList<String>>的一般方法如下:

public ArrayList<ArrayList<String>> readFile(String filename) throws IOException
{
    FileInputStream fis = new FileInputStream(filename);
    Scanner sc = new Scanner(fis);
    ArrayList<ArrayList<String>> modulesList = new ArrayList<>();

    int numModules = sc.nextInt();



    for (int i = 0; i < numModules; i++)
    {
        int numStringsPerModule = sc.nextInt();

        ArrayList<String> moduleEntries = new ArrayList<>();
        for (int j = 0; j < numStringsPerModule; j++)
        {
            String entry = sc.next();
            moduleEntries.add(entry);
        }
        modulesList.add(moduleEntries);
    }

    return modulesList;
}

You should be able to tailor this code to your specific situation. 您应该能够根据自己的具体情况定制此代码。 The general idea is that you will need to use a nested for loop, with the inner loop reading in a single module. 通常的想法是,您将需要使用嵌套的for循环,并且将内部循环读入单个模块中。

What about the following? 接下来呢?

 public ArrayList<Student> loadStudents(Scanner infile) throws FileNotFoundException{
    ArrayList<Student> students = new ArrayList<Student>();
    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);
    }
    return studtends;
}   

And

public List<Module> loadModules(String fileName) throws FileNotFoundException{
    Scanner infile =new Scanner(new InputStreamReader 
            (new FileInputStream(fileName)));
    int num=infile.nextInt();infile.nextLine();
    List<Module> ret = List<Module>();
    for (int i=0;i<num;i++){
        String c=infile.nextLine();
        ArrayList<Student> a =loadStudents(infile);

        Module m = new Module(c,a);
            ret.add(m);
    }
    infile.close();
    return ret;
}

The Module constructor would be better with List. 使用List可以更好地使用Module构造函数。 Do not use implementations in interfaces if possible. 如果可能,不要在接口中使用实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM