Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving Specific Entries from ArrayList
    primarykey
    data
    text
    <p>I have an array list with the following sample entries (Name &amp; Disease):</p> <pre><code>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 </code></pre> <ul> <li><strong>Same Name, Different Disease -> Remove Both!</strong> </li> <li><strong>One Instance -> Keep</strong></li> <li><strong>Same Name, Same Disease -> Keep, but only one copy!</strong></li> </ul> <p>Now, for some reason, .equals isn't working. So I can't simply do <code>if (arrayList.get(i).equals(arrayList.get(j)) then remove</code>. So I'm comparing names and diseases individually, using <code>compareTo</code> for comparing the diseases (which is working).</p> <p>Here is what I tried:</p> <blockquote> <pre><code> for (int i = 0; i &lt; IDArray.size(); i++){ //IDArray contains all the elements int countFound = 0; IdenPerson curr1 = IDArray.get(i); for (int j = i + 1; j &lt; 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? } } } </code></pre> </blockquote> <pre><code>public static class IdenPerson { String name; String dis; } </code></pre> <p>Using the above, I can find the double copy elements but I cannot separate the single instance elements. Please help! I cannot use libraries external to Java.</p> <p>Here's what the above ArrayList should look like when it works:</p> <pre><code>1. Name: Sebastian Cutting. Disease: Gingivitis 2. Name: Juan Weiss. Disease: Acquired Immunodeficiency Sydrome (AIDS) 3. Name: Lyndsey Stapleton. Disease: Chlamydia </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload