输入和输出递归java扫描程序

时间:2015-03-17 22:43:18

标签: java recursion

我似乎很难理解文本文件的输入和输出。我不确定我是如何在我创建的两个递归方法中实现输入文本文件的。我应该在构造函数中创建文本文件吗?任何帮助将非常感激。

import java.util.*;
import java.io.*;

/**
 * @author (Matt Mastell)
 * @version (3/14/15)
 */
public class CS152 {
    static final int MAXSIZE = 22;
    private static int size = 0;
    ArrayList<Student> Slist = new ArrayList<Student>(MAXSIZE);

    Scanner scann = new Scanner(new File("input.txt"));

    public static Student[] createList(Scanner scan) {
        Student[] list = new Student[MAXSIZE];
        return populateList(list, scan);
    }

    private static Student[] populateList(Student[] list, Scanner scan) {
        Student s;
        if (size < MAXSIZE && scan.hasNext()) {
            s = new Student(scan.next(), scan.next(), scan.next(),
                    scan.nextDouble(), scan.nextInt());
            list[size] = s;
            size++;
            return populateList(list, scan);
        }
        else {
            return list;
        }
    }

    public static int getSize() {
        return size;
    }
}

0 个答案:

没有答案