如何创建一个arraylist循环以将信息添加到对象

时间:2015-09-16 02:03:54

标签: java object arraylist

我的readData方法有很多代码,我必须单独键入每个对象以向其中添加信息。无论我尝试多少次,我都无法弄清楚如何将arrayList放到这里。

有人可以告诉我如何制作一个arraylist,我将能够使用循环添加所有这些信息。感谢

编辑readData方法是我在为正在添加信息的对象创建循环时遇到的问题。有没有办法可以循环它,所以我不需要有太多的信息并保留这部分

        person1.setStudentID(studentID);
        inputFile.nextLine();
        fullName = inputFile.nextLine();
        person1.setFullName(fullName);
        testScore = inputFile.nextDouble();
        person1.setTestScore(testScore);
        person1.setGrade(testScore);
        averageScore = averageScore + testScore;

        studentID = inputFile.nextInt();
        person2.setStudentID(studentID);
        inputFile.nextLine();
        fullName = inputFile.nextLine();
        person2.setFullName(fullName);
        testScore = inputFile.nextDouble();
        person2.setTestScore(testScore);
        person2.setGrade(testScore);
        averageScore = averageScore + testScore;
public class GradeDriver {

    public static void main(String[] args) {
        final String STUDENT_INFO = "HW1_Students.txt";

        List<GradeInfo> list = new ArrayList<GradeInfo>();

        Scanner inputFile = null;
        GradeInfo person1 = new GradeInfo();
        GradeInfo person2 = new GradeInfo();
        GradeInfo person3 = new GradeInfo();
        GradeInfo person4 = new GradeInfo();
        GradeInfo person5 = new GradeInfo();
        GradeInfo person6 = new GradeInfo();
        GradeInfo person7 = new GradeInfo();
        GradeInfo person8 = new GradeInfo();
        GradeInfo person9 = new GradeInfo();
        GradeInfo person10 = new GradeInfo();
        GradeInfo calculatedValues = new GradeInfo();

        list.add(person1);
        list.add(person2);
        list.add(person3);
        list.add(person4);
        list.add(person5);
        list.add(person6);
        list.add(person7);
        list.add(person8);
        list.add(person9);
        list.add(person10);

        try {
            inputFile = new Scanner(new File(STUDENT_INFO));
        }

        catch (FileNotFoundException ex) {
            System.out.println("\n *** Exception occured while opening "
                    + ex.getMessage() + " ***");
            System.exit(-1);
        }

        readData(inputFile, STUDENT_INFO, person1, person2, person3, person4,
                person5, person6, person7, person8, person9, person10,
                calculatedValues);

        System.out.println(calculatedValues.getAverage());
        //Get Standard Deviation
        standardDeviation(person1, person2, person3, person4, person5, person6,
                person7, person8, person9, person10, calculatedValues);
        //Print Out the Standard Deviation value
        System.out.println(calculatedValues.getStandardDeviation());

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

    }

    public static void readData(Scanner inputFile, final String STUDENT_INFO,
            GradeInfo person1, GradeInfo person2, GradeInfo person3,
            GradeInfo person4, GradeInfo person5, GradeInfo person6,
            GradeInfo person7, GradeInfo person8, GradeInfo person9,
            GradeInfo person10, GradeInfo calculatedValues) {

        GradeInfo standardDeviation = new GradeInfo();
        int studentID = 0;
        String fullName = "";
        double testScore = 0, averageScore = 0, scoreHolder = 0;

        while (inputFile.hasNext()) {
            try {

                studentID = inputFile.nextInt();
                person1.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person1.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person1.setTestScore(testScore);
                person1.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person2.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person2.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person2.setTestScore(testScore);
                person2.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person3.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person3.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person3.setTestScore(testScore);
                person3.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person4.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person4.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person4.setTestScore(testScore);
                person4.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person5.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person5.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person5.setTestScore(testScore);
                person5.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person6.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person6.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person6.setTestScore(testScore);
                person6.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person7.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person7.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person7.setTestScore(testScore);
                person7.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person8.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person8.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person8.setTestScore(testScore);
                person8.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person9.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person9.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person9.setTestScore(testScore);
                person9.setGrade(testScore);
                averageScore = averageScore + testScore;

                studentID = inputFile.nextInt();
                person10.setStudentID(studentID);
                inputFile.nextLine();
                fullName = inputFile.nextLine();
                person10.setFullName(fullName);
                testScore = inputFile.nextDouble();
                person10.setTestScore(testScore);
                person10.setGrade(testScore);
                averageScore = averageScore + testScore;

                averageScore = averageScore / 10;

                calculatedValues.setAverage(averageScore);

            } catch (InputMismatchException ex) {
                System.out
                        .println("\n *** Data doesn't match what the program expected -- "
                                + ex.toString() + " ***");
                inputFile.close();
                System.exit(-1);
            }

        } // try

    }

    public static void standardDeviation(GradeInfo person1, GradeInfo person2,
            GradeInfo person3, GradeInfo person4, GradeInfo person5,
            GradeInfo person6, GradeInfo person7, GradeInfo person8,
            GradeInfo person9, GradeInfo person10, GradeInfo calculatedValues) {
        double a, b, c, d, e, f, g, h, i, j, standardDeviation;
        int x = 0;
        double sum = 0;

        a = Math.pow(person1.getTestScore() - calculatedValues.getAverage(), 2);
        b = Math.pow(person2.getTestScore() - calculatedValues.getAverage(), 2);
        c = Math.pow(person3.getTestScore() - calculatedValues.getAverage(), 2);
        d = Math.pow(person4.getTestScore() - calculatedValues.getAverage(), 2);
        e = Math.pow(person5.getTestScore() - calculatedValues.getAverage(), 2);
        f = Math.pow(person6.getTestScore() - calculatedValues.getAverage(), 2);
        g = Math.pow(person7.getTestScore() - calculatedValues.getAverage(), 2);
        h = Math.pow(person8.getTestScore() - calculatedValues.getAverage(), 2);
        i = Math.pow(person9.getTestScore() - calculatedValues.getAverage(), 2);
        j = Math.pow(person10.getTestScore() - calculatedValues.getAverage(), 2);

        double[] array = { a, b, c, d, e, f, g, h, i, j };

        while (x < array.length) {
            sum = sum + array[x];
            x++;
        }

        sum = sum / 9;

        standardDeviation = Math.sqrt(sum);
        calculatedValues.setStandardDeviation(standardDeviation);

    }

}

1 个答案:

答案 0 :(得分:0)

以下是我认为您正在寻找的循环。顶部循环将使您能够初始化GradeInfo对象并将它们直接添加到arraylist。第二个循环将所有信息放入存储在arraylists中的对象中。您可能需要根据您的具体需求进行调整,但这应该会给您一个良好的开端。

ArrayList<GradeInfo> list = new ArrayList<GradeInfo>();
    for(int i = 0; i < 10; i++)
    {
        GradeInfo gInfo = new GradeInfo();
        list.add(gInfo);
    }

    int testScore;
    ArrayList<GradeInfo> list = new ArrayList<GradeInfo>();
    for(int x = 0; x < list.size(); x++)
    {

        list.get(x).setStudentID(inputFile.nextInt());
        inputFile.nextLine();
        list.get(x).setFullName(inputFile.nextLine();
        testScore = inputFile.nextDouble();
        list.get(x).setTestScore(testScore);
        list.get(x).setGrade(testScore);
        averageScore = averageScore + testScore;


    }