【问题标题】:Java Load in an ArrayList of ArrayList using scannerJava 使用扫描器加载 ArrayList 的 ArrayList
【发布时间】:2014-02-02 19:38:08
【问题描述】:

我正在尝试使用扫描仪加载模块的 ArrayList,然后每个模块还包含一个为该模块注册的学生的 ArrayList。这是我的模块构造函数:

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


}

数据将从一个布局如下的文本文件中加载:

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 

我已经能够像这样使用扫描仪加载学生:

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

}   

但我正在努力加载其中包含 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);

但这显然行不通。这里的任何想法或提示将不胜感激

【问题讨论】:

  • 您确定要复制粘贴相同的代码吗?导致ArrayList&lt;Student&gt; a = infile.next(); 这一行将无法编译。
  • 是的,这就是它不起作用的问题。这是我试图让它工作的尝试,但显然它不起作用。
  • 请正确复制粘贴loadModules代码。
  • 这就是 loadModules 的全部内容,我不知道它应该是什么样子

标签: java arraylist load


【解决方案1】:

infile.next() 返回一个String 值,因此它不能填充ArrayList&lt;Student&gt;。从文本文件中填充ArrayList&lt;ArrayList&lt;String&gt;&gt; 的一般方法可能如下:

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;
}

您应该能够根据您的具体情况定制此代码。一般的想法是您需要使用嵌套的for 循环,内部循环在单个模块中读取。

【讨论】:

  • 需要吗?我怀疑。我更喜欢使用原始海报尝试的方法来解析子结构。
  • 仍然会有一个内部for循环;它只是隐藏在方法调用中。我是从算法的角度讲的,而不是语义的。大 O 还是一样的。
【解决方案2】:

接下来呢?

 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;
}   

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;
}

Module 构造函数使用 List 会更好。尽可能不要在接口中使用实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2019-11-07
    • 2019-07-11
    相关资源
    最近更新 更多