具体条目我有下面的示例条目(名称&病)一个数组列表:去除ArrayList中
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
如果有两个条目名称相同,但不同的疾病,*哪个*你想被删除? – 2013-04-09 06:19:22
我想他们都被删除或不复制到另一个ArrayList。如果它们是完全重复的,我想打印/复制其中的一个。 – TookTheRook 2013-04-09 06:20:23
它看起来像名称地图 - >疾病(或疾病列表)会更好。 – BobTheBuilder 2013-04-09 06:21:12