0

I currently have some array lists and am trying to transfer some elements into a different array.

Here's what I have so far:

//create array separate from user input to remove duplicates 
ArrayList<String> idsWithoutDuplicates  = new ArrayList<String>();
for (int i = 0; i < missionIds.size(); i++){
    if (idsWithoutDuplicates.contains(missionIds.get(i))== false)
        idsWithoutDuplicates.add(missionIds.get(i)); 
}

//search for mission numbers within array of array 
for (int k = 0; k < idsWithoutDuplicates.size(); k++ ){
    int idSize=0;
    for (int employeeName = 0; employeeName <ids.size(); employeeName++){
        for (int employeeId = 0; employeeId <ids.get(employeeName).size(); employeeId++){
            /* when id is located, use the employee array to locate the other missions to 
             * which they belong */
            if (ids.get(employeeName).get(employeeId).equals(idsWithoutDuplicates.get(k))){
                for (employeeId = 0; employeeId < ids.get(employeeName).size(); employeeId++){
                    ArrayList<String> count  = new ArrayList<String>(); // ---> separate array for each employees total missions
                    if (ids.get(employeeName).get(employeeId).equals(idsWithoutDuplicates.get(k)) == false && 
                        count.contains(ids.get(employeeName).get(employeeId))== false){
                        count.add(ids.get(employeeName).get(employeeId)); 

                        idSize += count.size(); 

                        for (int i= 0; i < count.size(); i++){
                            System.out.print(count.get(i));
                        }
                    }
                }
            }
        }
    }
    System.out.println(idsWithoutDuplicates.get(k) + "\t    " + idSize);

}

The last if statement is trying to eliminate any duplicates with the contains condition, but this doesn't seem to work. I've tried converting 'count' to a set but this didn't work either.

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • 5
    Functions are your friend. Five nested for loops are not. – blur0224 Jun 03 '16 at 17:49
  • 1
    Why don't you use a `Set` instead of an `ArrayList`. It is meant to not contain duplicates. – Orin Jun 03 '16 at 17:51
  • I'd add the contents of the List to a Set, guaranteed to be unique. Implement hashCode and equals properly and it's easy. OR I'd use Java 8 lambdas: http://stackoverflow.com/questions/23699371/java-8-distinct-by-property – duffymo Jun 03 '16 at 17:51
  • You have 3 conditions inside the if. Check which condition does noy apply and you will have your answer – aviad Jun 03 '16 at 17:52
  • You don't want any duplicates within the arraylist right? Why don't you check the arraylist for the value you are about to enter? If the value is already there, don't add it. If the value is not already there, add it. – Tyler Jun 03 '16 at 17:53
  • the definition `ArrayList count` should be outside the for, no? – fhofmann Jun 03 '16 at 17:57

0 Answers0