我有一个包含以下示例条目(名称和疾病)的数组列表:
1. Name: Aimee Cholmondeley. Disease: German measles
2. Name: Colin Anissina. Disease: Diphtheria
3. Name: Colin Anissina. Disease: Malaria
4. Name: Aimee Cholmondeley. Disease: Typhoid fever
5. Name: Isaias Cheung. Disease: Haemophilus Influenza
6. Name: Isaias Cheung. Disease: Scarlet fever
7. Name: Sebastian Cutting. Disease: Gingivitis
8. Name: Juan Weiss. Disease: Acquired Immunodeficiency Sydrome (AIDS)
9. Name: Kaelyn Nauman. Disease: Amebiasis
10. Name: Kaelyn Nauman. Disease: Human Pulmonary Syndrome (HPS)
11. Name: Lyndsey Stapleton. Disease: Chlamydia
12. Name: Lyndsey Stapleton. Disease: Chlamydia
- 相同的名称,不同的疾病-> 删除两者!
- 一个实例 -> 保留
- 相同的名字,相同的疾病 -> 保留,但只有一份!
现在,由于某种原因, .equals 不起作用。所以我不能简单地做if (arrayList.get(i).equals(arrayList.get(j)) then remove
。所以我单独比较名称和疾病,compareTo
用于比较疾病(这是有效的)。
这是我尝试过的:
for (int i = 0; i < IDArray.size(); i++){ //IDArray contains all the elements int countFound = 0; IdenPerson curr1 = IDArray.get(i); for (int j = i + 1; j < IDArray.size(); j++) { IdenPerson curr2 = IDArray.get(j); if (curr1.name.toString().equals(curr2.name.toString())) { //If Name is same if ((curr1.dis.toString().compareTo(curr2.dis.toString())) == 0) { // And Disease is same System.out.println(curr1.name.toString()); // Print that Name break; } } else { // If name is not same, and only repeated once ... how to do this? } } }
public static class IdenPerson {
String name;
String dis;
}
使用上面的方法,我可以找到双副本元素,但我无法分离单个实例元素。请帮忙!我不能使用 Java 外部的库。
以下是上面的 ArrayList 工作时的样子:
1. Name: Sebastian Cutting. Disease: Gingivitis
2. Name: Juan Weiss. Disease: Acquired Immunodeficiency Sydrome (AIDS)
3. Name: Lyndsey Stapleton. Disease: Chlamydia