【问题标题】:How do I loop through an arrayList to store them as different objects?如何循环遍历 arrayList 以将它们存储为不同的对象?
【发布时间】:2017-11-22 06:18:43
【问题描述】:

我有一个字符串数组列表。

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

listGroup 的一个元素是 "CyberHacking Tools,CLS,Tim Hemmingway,2,P132303,Tyler Johnson,APP,M,P132304,Patty Henderson,APP,F"

我需要将前五个元素存储到一个项目对象中,该项目对象在项目类中具有构造函数,同时循环其余元素以将它们存储到 Student 类中具有构造函数的 Student 对象中。学生对象仅保存 4 个参数,每四个参数后,它将存储一个新的学生对象。因此,所有这些对象都将传递到学生和项目列表中。

这些对象的代码写在下面。

在项目类中:

    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;
    }

这是项目对象:

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

我分别有一个项目类、学生类、FileReader类和JFrame类。解决这个问题的最佳方法是什么?

谢谢。

【问题讨论】:

  • 您能否解释一下如何在四个参数之后创建新的学生对象,正如您在问题“学生对象仅包含 4 个参数,每四个参数之后,它将存储一个新的学生对象”中提到的那样。
  • 你的意思是整个"CyberHacking Tools,CLS,..."listGroup中的字符串之一,还是每个逗号分隔的字符串都是列表中的一个元素?
  • 元素的数量总是正确的吗?可以有 12 个元素,其中最后 3 个不能形成学生对象吗?
  • noOfProject 参数是什么?
  • @Sweeper 我认为2 是参数noOfProject 的值,而NoOfStudent 参数的值在输入中不存在。我们需要通过解析输入字符串来计算学生人数,如答案部分所示。

标签: java arrays object arraylist data-structures


【解决方案1】:

我假设您想要存储项目和学生对象以供以后使用。以下是您可以采取的方法:

        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]));
                    }
                }

注意:在创建PersonStudent 的对象时,我任意传递了参数,假设strings 的顺序是一致的。希望你能按照你的构造函数参数顺序传递参数。

【讨论】:

  • 我确定您不需要计算 noOfStudents。
  • @ajc 原因...?因为构造函数我用过:public Project(int noOfProj,String title, String sch, String superv, intNoOfStudent)
  • 如果您查看输入,CyberHacking Tools,CLS,Tim Hemmingway,2 从逻辑上讲,理想情况下最后一个参数应该是 noOfStudents
  • @ajc 那是因为noOfProj参数
  • 这只是我的观点 - 但我没有看到输入和 noOfProj 参数之间的任何映射。 Op 必须确认。
【解决方案2】:

首先,您的Project 构造函数似乎有4 参数而不是5。话说,

    // 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. 
    }

【讨论】:

  • 根据您的假设new Project(tuple[0], tuple[1], tuple[2], tuple[3]); 应该显示未定义的构造函数。因为你的最后一个参数应该是int
  • 是的,这不是工作代码。这是解决问题的指南。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多