简体   繁体   中英

How do I loop through an arrayList to store them as different objects?

I have an Arraylist of String.

private List<String> listGroup = new ArrayList<String>();

One elements of listGroup is "CyberHacking Tools,CLS,Tim Hemmingway,2,P132303,Tyler Johnson,APP,M,P132304,Patty Henderson,APP,F" .

I need to store the first five elements into a project object which has constructor in project class, while looping the rest to store them into Student object with constructors in Student class. The student object only holds 4 parameters and after every four, it will store a new student object. All of these objects will hence be passed into a Student and Project list.

The codes for these objects are written below.

In the Project class:

    public Project(int noOfProj, String title, String sch, String superv, int NoOfStudent) {
        this.noOfProj = noOfProj;
        this.title = title;
        this.school = sch;
        this.supervisorName = superv;
        this.noOfStudent = NoOfStudent;
        // this.projIndex = projCode;
    }

This is the Project object:

Project p = new Project(title, school, supervisorName, noOfStudents);

I have a project class, student class,FileReader class and JFrame class respectively. What is the best way to go about this?

Thank you.

I will assume you want to store the project and stundent objects for later usage. Here's the approach you can take:

        List<Project> personList = new ArrayList<Project>(); //store list of projects
                    List<Student> listStudent = new ArrayList<Student>(); //store list of students

               for (String str : listGroup) {

                    String arr[] = str.split(",");
                    //as student object takes 4 arguements and "noOfStudents" is the number of "Student" objects found in the string
                    int noOfStudents = (arr.length / 4)-1;
                    Project p = new Project(Integer.parseInt(arr[3]),arr[0],arr[1],arr[2],noOfStudents);
                    personList.add(p);
                    for(int i=4;i<arr.length-4;i+=4)
                    {
                        listStudent.add(new Student(arr[i],arr[i+1],arr[i+2],arr[i+3]));
                    }
                }

NB: While creating the objects of Person and Student I've passed parameters arbitrarily assuming the sequence of the string s will be consistent. Hope you can pass the parameters according to your constructor parameter sequence.

First thing, your Project constructor seems to have 4 parameters and not 5 . Said that,

    // considering this as your sample line -
    // String line = "CyberHacking Tools,CLS,Tim Hemmingway,2,P132303,Tyler Johnson,APP,M,P132304,Patty Henderson,APP,F"

    String[] tuple = line.split(",");

    // Get first 4 tuples for Project. 
    // CyberHacking Tools,CLS,Tim Hemmingway,2
    Project project = new Project(tuple[0], tuple[1], tuple[2], tuple[3]);

    // Iterate over rest of the tuples for Student. 
    // P132303,Tyler Johnson,APP,M 
    // P132304,Patty Henderson,APP,F
    for (int i = 4; i < tuple.length; i += 4) {
        Student student = new Student(tuple[i], tuple[i + 1], tuple[i + 2], tuple[i + 3]);
        // add the student object to List<Student> here. 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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