具體條目我有下面的示例條目(名稱&病)一個數組列表:去除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