如何从另一个arraylist迭代一个arraylist并想显示如下所示的值?

时间:2015-05-28 05:45:36

标签: java

样本输出:

Department name : CSE
Student Count   : 4
Department name : EEE
Student Count   : 2

我的代码

public class Student {

    private int id;
    private String name;
    private String department;

    public Student(int id, String name, String department) {
        this.id = id;
        this.name = name;
        this.department = department;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

}


public class Department {


    private String name;
    private int studentCount;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStudentCount() {
        return studentCount;
    }

    public void setStudentCount(int studentCount) {
        this.studentCount = studentCount;
    }


}


public class Main {

    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();
        students.add(new Student(1, "aaa", "CSE"));
        students.add(new Student(2, "bbb", "EEE"));
        students.add(new Student(3, "bbb", "IT"));
        students.add(new Student(4, "ddd", "CSE"));
        students.add(new Student(5, "eee", "MC"));
        students.add(new Student(6, "fff", "IT"));
        students.add(new Student(7, "ggg", "EEE"));
        students.add(new Student(8, "hhh", "CSE"));
        students.add(new Student(9, "iii", "IT"));
        students.add(new Student(10, "jjj", "EC"));
        students.add(new Student(11, "kkk", "CSE"));


        List<Department> departments = new ArrayList<Department>();

//The output should be printed from here

    }
}

3 个答案:

答案 0 :(得分:0)

试试此代码

        List<Department> departments = new ArrayList<Department>();
        Department dep = new Department();
        dep.setName("CSE");
        dep.setStudentCount(4);
        departments.add(dep);

        dep = new Department();
        dep.setName("EEE");
        dep.setStudentCount(2);
        departments.add(dep);

        for(Department dept : departments) {
            System.out.println("Department Name : " + dept.getName());
            System.out.println("Student Count   : " + dept.getStudentCount());
        }

<强>输出

  

部门名称:CSE

     

学生人数:4

     

部门名称:EEE

     

学生人数:2

答案 1 :(得分:0)

以下是您想要的简单示例。它将填充学生名单中的部门

public class DepartmentExample
{  

  public void doTheWork()
  {
    List<Student> students = new ArrayList<Student>();
    students.add(new Student(1, "aaa", "CSE"));
    students.add(new Student(2, "bbb", "EEE"));
    students.add(new Student(3, "bbb", "IT"));
    students.add(new Student(4, "ddd", "CSE"));
    students.add(new Student(5, "eee", "MC"));
    students.add(new Student(6, "fff", "IT"));
    students.add(new Student(7, "ggg", "EEE"));
    students.add(new Student(8, "hhh", "CSE"));
    students.add(new Student(9, "iii", "IT"));
    students.add(new Student(10, "jjj", "EC"));
    students.add(new Student(11, "kkk", "CSE"));

    List<Department> departments = populateDepartmentsFromStudentsList(students);

    for (Department department : departments)
    {
      System.out.println("Department name :" +department.getName() );
      System.out.println("Student Count   :" +department.getStudentCount() );
    }
  }

  private List<Department> populateDepartmentsFromStudentsList(List<Student> students)
  {
    List<Department> departments = new ArrayList<Department>();

    for (Student student : students)
    {
      Department department = getDepartmentByName(departments, student.department);
      if (department == null)
      {
        department = new Department();
        department.setName(student.getDepartment());
        departments.add(department);
      }
      department.setStudentCount(department.getStudentCount() + 1);
    }
    return departments;
  }

  private Department getDepartmentByName(List<Department> departments, String departmentName)
  {
    for (Department department : departments)
    {
      if (department.getName().equals(departmentName))
      {
        return department;
      }
    }
    return null;
  }

  public static void main(String[] args)
  {
    DepartmentExample departmentExample = new DepartmentExample();
    departmentExample.doTheWork();
  }

}

答案 2 :(得分:0)

首先,就OOP而言,如果您的Student类具有Department类引用,而不仅仅是部门字符串名称,则必须更好。这样两个对象之间的连接就会简单得多,而且你不必经历字符串比较的恐怖。

更好的是,只需让您的Department类对象持有List<Student>个对象,而不仅仅是计数。这样,你的联系就会更强大。

在您当前的实施中,例如,您可以让一个学生注册一个不存在的部门,并且部门中的学生人数多于实际连接的部门。

目前,由于两个对象之间没有真正的连接,因此您的输出似乎应该是扫描一个列表 - 部门列表:

for (Department department : departments) {
    System.out.println("Department name : " + department.getName());
    System.out.println("Student Count   : " + department.getStudentCount());
}

如果您的问题是如何在给出学生列表的情况下填充您的部门列表,那么我会保留一张地图然后填充该列表:

Map<String, Integer> studentsInDepartments = new HashMap<String, Integer>();

for (Student student : students) {
    String department = student.getDepartment();
    if (studentsInDepartments.containsKey(department)) {
        studentsInDepartments.put(department, studentsInDepartments.get(department) + 1);
    } else {
        studentsInDepartments.put(department, 1);
    }
}

for (String key : studentsInDepartments.keySet()) {
    Department department = new Department();
    department.setName(key);
    department.setStudentCount(studentsInDepartments.get(key));
}

正如你所看到的,这是“不那么漂亮”。我相信在这里和一般情况下,将对象结构更加面向对象会更好。