【发布时间】:2021-10-11 22:32:48
【问题描述】:
将对象数组添加到数组列表中,并根据一个属性值与其他数组列表进行比较并过滤结果。
我有一个对象 personDetails 和 departmentDetails 的数组。
package com.education;
import java.util.*;
public class educationMain {
public static void main(String[] args) {
List<person> list=new ArrayList<person>();
person l1 = new person(1,"Samual",100,"Sales","Business");
person l2 = new person(2,"Alex",100,"Sales","Business");
person l3 = new person(3,"Bob",101,"Engineering","Technology");
person l4 = new person(4,"Michel",101,"Engineering","Technology");
person l5 = new person(5,"Ryan",102,"PR","Services");
person l6 = new person(6,"Horward",103,"Leadership","Managmnet");
person l7 = new person(7,"Cyna",104,"HR","Human Resource");
list.add(l1);
list.add(l2);
list.add(l3);
list.add(l4);
list.add(l5);
list.add(l6);
list.add(l7);
for(person b:list){
System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName);
}
List<department> depList = new ArrayList<department>();
department d1 = new department(100, "Sales","Business");
department d2 = new department(101, "Engineering","Technology");
department d3 = new department(102, "PR","Services");
depList.add(d1);
depList.add(d2);
depList.add(d3);
for(department b:depList){
System.out.println(b.deptCode+" "+b.parentDept+" "+b.deptName);
}
}
}
所以上面的代码工作正常并且显示正确。
我的人物类
包 com.education;
public class person {
public int personId;
public String name;
private int deptCode;
private String parentDept;
private String deptName;
public person(int personId, String name, int deptCode, String parentDept, String deptName) {
super();
this.personId = personId;
this.name = name;
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void display(){
System.out.println(" " + personId + " " +name + " " + deptCode+ " " + parentDept + " "+deptName);
System.out.println();
}
}
我的系类
public class department {
private int deptCode;
private String parentDept;
private String deptName;
public department(int deptCode, String parentDept, String deptName) {
super();
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void dispalyDepartment() {
System.out.println(" "+deptCode+" "+parentDept+" "+deptName);
}
}
现在我的目标是将 personDetails 和 departmentDetails 放入 arraylist 并进行比较,然后根据部门代码找出差异。
这是我的逻辑:工作正常。
List<person> listC = new ArrayList<person>();
for(person p : list) {
boolean flag = false;
for (department d:depList) {
if(p.deptCode == d.deptCode) {
flag = false;
break;
}else {
flag = true;
}
}
if(flag == true) {
listC.add(p);
}
}
o/p 应该是这样的
(6,"Horward",103,"Leadership","Managmnet");
(7,"Cyna",104,"HR","Human Resource");
因为 deptCode : 103 和 104 不存在。
有人可以帮我吗?我可以使用任何其他收集技术吗?
【问题讨论】:
-
您的代码对我来说运行良好。请检查您是否输入了所有有问题的信息。
-
如何根据
deptCode找到personDetails和personDetails的区别。是的,打印工作正常。但我需要一个差异,因此我将它放入对象的数组中。 -
是的,我添加了
person和department类。现在从这里我想将这个对象数组添加到数组列表中,然后比较两个数组列表。如果有任何更聪明的方式,请欣赏。 -
您是否尝试过按部门代码比较两个数组?请添加您尝试过但不起作用的代码。
-
No Actuay 首先我不确定天气是否能得到正确的列表,所以到目前为止我还没有写任何东西来通过部门代码对两个数组进行映射。请帮帮我。
标签: java arraylist collections