【问题标题】:Iterating through an ArrayList of Objects and printing each variable from each object遍历对象的 ArrayList 并打印每个对象的每个变量
【发布时间】:2017-09-06 17:53:34
【问题描述】:

您好,我在打印对象的 ArrayList 时遇到问题。我想从 CSV 文件中导入值(这可行),然后将值保存到我创建的对象(StudentInfo)中,然后我想遍历并打印出每个对象的信息。

我已经研究过但无法解决这个问题,我在代码中更改了一些内容,并且能够按预期打印一个对象,这似乎是存储到 ArrayList 中的最后一个对象。

我跟踪了我的代码(在 Eclipse 中使用调试器),发现它只进入了一次 while 循环,我不明白为什么。

我相信问题出在我的 printResults() 方法中,尽管我无法弄清楚为什么它只会进入一次 while 循环(正如我之前所说,它似乎只是 ArrayList 的最后一个索引)。

请查看以下代码块。感谢您抽出宝贵时间查看此内容,并感谢您提供的任何帮助。这是作业,所以我们被告知要使用分词器(尽管教授说我们将来会学习更好的方法)。

package excercise12Dot1;

public class StudentInfo {
    private String type;
    private String fName;
    private String lName;
    private double testOne;
    private double testTwo;
    private double testThree;
    private double finalGrade;

    public StudentInfo() {
        this.type = "";
        this.fName = "";
        this.lName = "";
        this.testOne = 0;
        this.testTwo = 0;
        this.testThree = 0;
        this.finalGrade = 0;
    }

    public StudentInfo(String type, String fname, String lName, double testOne, double testTwo, double testThree, double finalGrade){
        this.type = type;
        this.fName = fname;
        this.lName = lName;
        this.testOne = testOne;
        this.testTwo = testTwo;
        this.testThree = testThree;
        this.finalGrade = finalGrade;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public double getTestOne() {
        return testOne;
    }

    public void setTestOne(double testOne) {
        this.testOne = testOne;
    }

    public double getTestTwo() {
        return testTwo;
    }

    public void setTestTwo(double testTwo) {
        this.testTwo = testTwo;
    }

    public double getTestThree() {
        return testThree;
    }

    public void setTestThree(double testThree) {
        this.testThree = testThree;
    }

    public double getFinalGrade() {
        return finalGrade;
    }

    public void setFinalGrade(double finalGrade) {
        this.finalGrade = finalGrade;
    }
}

package excercise12Dot1;
import java.io.File;
import java.util.StringTokenizer;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class NonCreditCourseGrades {
    private Scanner csvReader;
    private int counter = 0;
    private ArrayList<StudentInfo> finalStudentGradesArray;
    public void openFile(){
        try{
            csvReader = new Scanner(new File("StudentsScores.csv"));
            while (csvReader.hasNext()){
                String studentRecord = csvReader.nextLine();
                StringTokenizer tokenizer = new StringTokenizer(studentRecord, ",");
                String type = tokenizer.nextToken();
                String fName = tokenizer.nextToken();
                String lName = tokenizer.nextToken();
                double testOne = Double.parseDouble(tokenizer.nextToken());
                double testTwo = Double.parseDouble(tokenizer.nextToken());
                double testThree = Double.parseDouble(tokenizer.nextToken());
                double finalScore = 0;
                StudentInfo newStudent = new StudentInfo(type, fName, lName, testOne, testTwo, testThree, finalScore);
                finalStudentGradesArray = new ArrayList<StudentInfo>();
                finalStudentGradesArray.add(newStudent);
                ++counter;
            }   
        }catch(FileNotFoundException ex){
            System.err.println("FILE NOT FOUND");
        }
        csvReader.close();
    }
    public void printResults(){
        Iterator<StudentInfo> itr = finalStudentGradesArray.iterator();
        while (itr.hasNext()){
            StudentInfo results = (StudentInfo)itr.next();
            System.out.println("Student Type:\t" + results.getType() + "\nFirst Name:\t" + results.getfName() + "\nLast Name:\t" + results.getlName() + "\nTest 1:\t\t" + results.getTestOne() + "\nTest 2:\t\t" + results.getTestTwo() + "\nTest 3:\t\t" + results.getTestThree() + "\nFinal Score: \t" + results.getFinalGrade() + "\n\n");
        }
    }

package excercise12Dot1;


public class NonCreditCourseGradesRunner {

    public static void main(String[] args) {
        NonCreditCourseGrades test = new NonCreditCourseGrades();
        test.openFile();
        test.printResults();
    }
}

【问题讨论】:

    标签: java arraylist iterator


    【解决方案1】:

    您通过读取 CSV 文件的循环每次迭代都重新创建了结果数组:

    finalStudentGradesArray = new ArrayList();

    【讨论】:

    • 是的。将 finalStudentGradesArray = new ArrayList&lt;StudentInfo&gt;(); 移到 while 循环之外或将其移至构造函数。
    • @David 谢谢,我刚刚在读取器循环之外实例化了 ArrayList 并删除了我在每次迭代中创建一个新 ArrayList 的那行代码。非常感谢大卫,这为我解决了这个问题。
    猜你喜欢
    • 2018-09-04
    • 2020-05-23
    • 1970-01-01
    • 2021-12-10
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多