安卓:比较两种对象ArrayList和从第二ArrayList中找到不匹配的IDS [英] Android : Compare two ArrayList of objects and find unmatching ids from the second ArrayList
问题描述
我想比较两种对象的ArrayList和基于对象的ID第二个ArrayList中找到不匹配的值。
I want to compare two ArrayList of objects and find the unmatching values from the second ArrayList based on the ids in the object.
例如:
Person.java
Person.java
private int id;
private String name;
private String place;
MainActivity.java:
MainActivity.java:
ArrayList<Person> arrayList1 = new ArrayList<Person>();
arrayList1.add(new Person(1,"name","place"));
arrayList1.add(new Person(2,"name","place"));
arrayList1.add(new Person(3,"name","place"));
ArrayList<Person> arrayList2 = new ArrayList<Person>();
arrayList2.add(new Person(1,"name","place"));
arrayList2.add(new Person(3,"name","place"));
arrayList2.add(new Person(5,"name","place"));
arrayList2.add(new Person(6,"name","place"));
我要比较的arrayList1,arrayList2需要找从arrayList2的不匹配的值。
我需要ID值5,6。
I want to compare the arrayList1, arrayList2 and need to find the unmatching values from the arrayList2. I need the id values 5,6.
我怎样才能做到这一点?
How can I do this?
推荐答案
您可以使用内循环,以检查是否从 arrayList2
人的ID对应于任何人的ID在 arrayList1
。你需要一个标志来标记,如果一些人发现。
You can use an inner loop, to check if the Person's id from arrayList2
corresponds to any Person id in the arrayList1
. You'll need a flag to mark if some Person was found.
ArrayList<Integer> results = new ArrayList<>();
// Loop arrayList2 items
for (Person person2 : arrayList2) {
// Loop arrayList1 items
boolean found = false;
for (Person person1 : arrayList1) {
if (person2.id == person1.id) {
found = true;
}
}
if (!found) {
results.add(person2.id);
}
}
这篇关于安卓:比较两种对象ArrayList和从第二ArrayList中找到不匹配的IDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!