在链接列表中存储对象

时间:2019-03-06 08:36:17

标签: java list object

我正在创建一个简单的程序,该程序从文本文件读取数据并将其显示在控制台中。我要显示的数据是有关学生的信息-名称,ID,主题,分数等

程序将读取文本文件,并为找到的每个用户创建一个学生对象。尝试将这些学生存储在链接列表中时遇到问题。似乎每次都会创建一个新列表,并覆盖前一个列表,因此我总是只在列表中找到一个学生。

如何获取它而不覆盖先前的列表?这是下面的一些代码:

public static boolean readFile(String filename) {
    File file = new File(filename);
    try {
    Scanner scanner = new Scanner(file);
    while(scanner.hasNextLine()){
     String[] words = scanner.nextLine().split(",");

     int id = Integer.parseInt(words[0]);
     String firstName = words[1];
     String lastName = words[2];
     int mathMark1 = Integer.parseInt(words[3]);
     int mathMark2 = Integer.parseInt(words[4]);
     int mathMark3 = Integer.parseInt(words[5]);
     int englishMark1 = Integer.parseInt(words[6]);
     int englishMark2 = Integer.parseInt(words[7]);
     int englishMark3 = Integer.parseInt(words[8]);

     addStudent(id,firstName,lastName,mathMark1,mathMark2,mathMark3,englishMark1,englishMark2,englishMark3);
     }
    scanner.close();
    } catch (FileNotFoundException e) {
    System.out.println("Failed to read file");
    }
     return true;
    }


private static void addStudent(int id, String firstName, String lastName,int
        mathsMark1, int mathsMark2, int mathsMark3, int englishMark1, int englishMark2,
        int englishMark3) {
    LinkedList<Student> student = new LinkedList<>();
    student.add(new Student(id,firstName,lastName));

    LinkedList<AssignmentMarks> mathematicsMarks = new LinkedList<>();
    mathematicsMarks.add(new AssignmentMarks("Mathematics",mathsMark1,mathsMark2,mathsMark3));

    LinkedList<AssignmentMarks> englishMarks = new LinkedList<>();
    englishMarks.add(new AssignmentMarks("English",englishMark1,englishMark2,englishMark3));
}

上面的代码在我的Main类中。下面的代码来自我的学生班:

public class Student {

    private int id;
    private String firstName;
    private String lastName;
    private AssignmentMarks mathMarks;
    private AssignmentMarks englishMarks;

    public Student(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFullName() {
        return firstName;
    }
}

任何帮助,谢谢大家!

2 个答案:

答案 0 :(得分:2)

此变量

LinkedList<Student> student = new LinkedList<>();

需要在方法外部,字段中或在readFile中声明并作为参数传递,否则将在您每次调用addStudent

时创建

答案 1 :(得分:1)

将您的LinkedList声明为该类的成员,因为每次调用addStudent()时,您都是在创建一个新列表。

您应该改为执行以下操作:

public class Test {

   private LinkedList<Student> student = new LinkedList<>();

   public static boolean readFile(String filename) {
      // ...
      addStudent(id,firstName,lastName,mathMark1,mathMark2,mathMark3,
                 englishMark1,englishMark2,englishMark3);

   }

   private static void addStudent(int id, String firstName, String lastName,int
        mathsMark1, int mathsMark2, int mathsMark3, int englishMark1, int englishMark2,
        int englishMark3) {
     // ...

     // this will now add it to the only instance of the list 
     student.add(new Student(id,firstName,lastName));
   }
}