简体   繁体   English

将 ArrayList 对象添加到不同对象类型的现有 ArrayList

[英]Adding an ArrayList Object to an existing ArrayList of different Object type

Hopefully my title makes sense.希望我的标题有意义。 What I am trying to do with my code is read a file line by line which has attributes about Students (Student ID, first name, last name, etc.) and the courses they've taken.我试图用我的代码做的是逐行读取一个文件,其中包含有关学生(学生 ID、名字、姓氏等)和他们所修课程的属性。 I'm pretty confident my buffered reader is fine but I'm a little lost when it comes to the specifications of my program.我非常有信心我的缓冲阅读器很好,但是当谈到我的程序规范时,我有点迷茫。 The issue comes about when I am adding CourseList ArrayList objects to my Student ArrayList objects.当我将 CourseList ArrayList 对象添加到我的 Student ArrayList 对象时,问题就出现了。 The code runs fine, but does not add the courseLists to the student objects.代码运行良好,但没有将 courseLists 添加到学生对象。 Essentially I am making 2 Student ArrayLists that should have the courses stored in them relating to them.本质上,我正在制作 2 个 Student ArrayLists,它们应该存储与它们相关的课程。 ie) Student1,Course1,Course2,etc..即)学生 1、课程 1、课程 2 等。

Thank you in advance, here is my code:在此先感谢您,这是我的代码:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class studentDir {
    static int currentStudent = 0;
    public static void main(String args[]) throws IOException{
        ArrayList<Student> students = new ArrayList<Student>();

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

        while(input.hasNextLine()) {

            String line = input.nextLine();
            StringTokenizer st = new StringTokenizer(line,",");

                while(st.hasMoreTokens()){
                    String first, last, idNo;
                    last = st.nextToken();
                    first = st.nextToken();
                    System.out.println("Added student: "+last+", "+first);
                    idNo = st.nextToken();
                    System.out.println("Stored ID: "+idNo);
                    Student s = new Student(last, first, idNo);
                    students.add(s);
                    line = input.nextLine();
                    st = new StringTokenizer(line,",");
                    while(st.hasMoreTokens()){
                        String x = st.nextToken();
                        System.out.println("If controller read in: "+x);
                        if(x.equals("-999")){
                            line = input.nextLine();
                            st = new StringTokenizer(line, ",");
                            System.out.println("Added Credits & GPA");
                            Float totalCredits = Float.parseFloat(st.nextToken());
                            Float gpa = Float.parseFloat(st.nextToken());
                            students.get(currentStudent).storeGPA(totalCredits, gpa);
                            System.out.println("GPA Read In : "+students.get(currentStudent).getGPA());
                            currentStudent++;
                        }
                        else{
                            System.out.println("Adding course.");
                            String courseID = x;
                            float credits = Float.parseFloat(st.nextToken());
                            String grade = st.nextToken();
                            System.out.println(x+", "+grade);
                            CourseList cl = new CourseList(courseID,grade,credits);
                            s.add(cl);
                            System.out.println(cl.toString());
                            System.out.println(courseID+" "+credits+" "+grade+" added.");
                            line = input.nextLine();
                            st = new StringTokenizer(line,",");
                            }
                        }

                    for(Student x : students)
                        System.out.println(x.toString());                       
                }

        }
        input.close();      
        currentStudent = 0;
    }
}

import java.util.ArrayList;

public class Student {
    private String firstName;
    private String lastName;
    private String studentID;
    private float gpa;
    private float totalCredits;
    private ArrayList<CourseList> courses = new ArrayList<>();

    Student(String y, String x, String z){
            this.firstName = x;
            this.lastName = y;
            this.studentID = z;
    }

        public String toString(){
            String x = (this.firstName+" "+this.lastName+" "+this.studentID+".");
            return x;
        }
        public void setGPA(float x){
            this.gpa = x;
        }

        public float getGPA(){
            return gpa;
        }

        public String getID(){
            return this.studentID;
        }

        public void gpaCalc(Student stu, String id){
            totalCredits = 0;

        }

        public void storeGPA(float tcredits, float gpa){
            this.gpa = gpa;
            this.totalCredits = tcredits;
        }
        public void add(CourseList cl) {

        }

}

public class CourseList {
    String idNo, grade, courseID;
    float credits;
    float gpa;

    public CourseList(String x, String y, float z) {
        this.courseID = x;
        this.grade = y;
        this.credits = z;
    }

    public String toString(){
        String x = ("Course ID: "+this.courseID+", Grade : "+this.grade+", Credits Earned : "+this.credits+".");
        return x;
    }

    public float getCredits() {
        return this.credits;
    }
}


Input: 
Jones,Mary,903452
4342,2.5,A
3311,4,B+
-999
6.5,3.569
Martin,Joseph,312345
4598,3,C
1122,3,A-
2467,4,A
-999
10,3.31

Output: 
Added student: Jones, Mary
Stored ID: 903452
If controller read in: 4342
Adding course.
4342, A
Course ID: 4342, Grade : A, Credits Earned : 2.5.
4342 2.5 A added.
If controller read in: 3311
Adding course.
3311, B+
Course ID: 3311, Grade : B+, Credits Earned : 4.0.
3311 4.0 B+ added.
If controller read in: -999
Added Credits & GPA
GPA Read In : 3.569
Mary Jones 903452.
Added student: Martin, Joseph
Stored ID: 312345
If controller read in: 4598
Adding course.
4598, C
Course ID: 4598, Grade : C, Credits Earned : 3.0.
4598 3.0 C added.
If controller read in: 1122
Adding course.
1122, A-
Course ID: 1122, Grade : A-, Credits Earned : 3.0.
1122 3.0 A- added.
If controller read in: 2467
Adding course.
2467, A
Course ID: 2467, Grade : A, Credits Earned : 4.0.
2467 4.0 A added.
If controller read in: -999
Added Credits & GPA
GPA Read In : 3.31
Mary Jones 903452.
Joseph Martin 312345.

Thank you in advance guys!提前谢谢你们!

IMHO, using ArrayList of two different types is not a good idea here.恕我直言,在这里使用两种不同类型的 ArrayList 不是一个好主意。 You should be using something like Map<Student, ArrayList<Course>> .你应该使用类似Map<Student, ArrayList<Course>> It is:这是:

  1. Human readable and thus maintainable.人类可读,因此可维护。
  2. Its type safe.它的类型安全。
  3. Enables operations like searching for all courses taken by any student to complete in O(1).启用诸如搜索任何学生选修的所有课程以在 O(1) 中完成的操作。

In your code snipped your addCourse Method is empty在您的代码中,您的 addCourse 方法为空

        public void add(CourseList cl) {

    }

I guess that is your problem here我想这是你的问题

Just set the ArrayList of CourseList to Student Class then it will work只需将 CourseList 的 ArrayList 设置为 Student Class 即可

public class studentDir {
                static int currentStudent = 0;
                public static void main(String args[]) throws IOException{
                    ArrayList<Student> students = new ArrayList<Student>();
    ArrayList<CourseList> courseLists=new ArrayList<>();
                    Scanner input = new Scanner(new File("WarmUpData.txt"));

                    while(input.hasNextLine()) {

                        String line = input.nextLine();
                        StringTokenizer st = new StringTokenizer(line,",");

                            while(st.hasMoreTokens()){
                                String first, last, idNo;
                                last = st.nextToken();
                                first = st.nextToken();
                                System.out.println("Added student: "+last+", "+first);
                                idNo = st.nextToken();
                                System.out.println("Stored ID: "+idNo);
                                Student s = new Student(last, first, idNo);

                                line = input.nextLine();
                                st = new StringTokenizer(line,",");
                                while(st.hasMoreTokens()){
                                    String x = st.nextToken();
                                    System.out.println("If controller read in: "+x);
                                    if(x.equals("-999")){
                                        line = input.nextLine();
                                        st = new StringTokenizer(line, ",");
                                        System.out.println("Added Credits & GPA");
                                        Float totalCredits = Float.parseFloat(st.nextToken());
                                        Float gpa = Float.parseFloat(st.nextToken());
                                        students.get(currentStudent).storeGPA(totalCredits, gpa);
                                        System.out.println("GPA Read In : "+students.get(currentStudent).getGPA());
                                        currentStudent++;
                                    }
                                    else{
                                        System.out.println("Adding course.");
                                        String courseID = x;

                                        float credits = Float.parseFloat(st.nextToken());
                                        String grade = st.nextToken();
                                        System.out.println(x+", "+grade);
                                        CourseList cl = new CourseList(courseID,grade,credits);
                                       courseLists.add(cl);
                                        s.setCourses(courseLists);
                                        System.out.println(cl.toString());
                                        System.out.println(courseID+" "+credits+" "+grade+" added.");
                                        line = input.nextLine();
                                        st = new StringTokenizer(line,",");

                                    }
                                    students.add(s);

                                    }

                                for(Student x : students)
                                    System.out.println(x);                       
                            }

                    }
                    input.close();      
                    currentStudent = 0;
                }
            }



     class Student {
        private String firstName;
        private String lastName;
        private String studentID;
        private float gpa;
        private float totalCredits;
        private ArrayList<CourseList> courses = new ArrayList<>();

        Student(String y, String x, String z) {
            this.firstName = x;
            this.lastName = y;
            this.studentID = z;
        }

        public String toString() {
            String x = (this.firstName + " " + this.lastName + " " + this.studentID + ".");
            return x;
        }

        public void setGPA(float x) {
            this.gpa = x;
        }

        public float getGPA() {
            return gpa;
        }

        public String getID() {
            return this.studentID;
        }

        public void gpaCalc(Student stu, String id) {
            totalCredits = 0;

        }

        public void storeGPA(float tcredits, float gpa) {
            this.gpa = gpa;
            this.totalCredits = tcredits;
        }



        public ArrayList<CourseList> getCourses() {
            return courses;
        }

        public void setCourses(ArrayList<CourseList> courses) {
            this.courses = courses;
        }

    }

    class CourseList {
        String idNo, grade, courseID;
        float credits;
        float gpa;

        public CourseList(String x, String y, float z) {
            this.courseID = x;
            this.grade = y;
            this.credits = z;
        }

        public String toString() {
            String x = ("Course ID: " + this.courseID + ", Grade : " + this.grade + ", Credits Earned : " + this.credits
                    + ".");
            return x;
        }

        public float getCredits() {
            return this.credits;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM