neeljoshi86
Technical User
Hi there. I have an ArrayList with a bunch of String values in it. I want to be able to check if a particular string appears X amount of times.
To add some context to the dilemma, the strings are date/times (in the format DD/MM/YYYY-HHMM) for which certain attendees are available for a meeting. For example, if two attendees are available on May 3rd 2004 at 9am, the ArrayList will be [03/05/2004-0900,03/05/2004-0900]. I want to be able to check through the arraylist and find out if the same string appears the same amount of times as there are attendees (meaning all attendees are free at that time).
My pseudo code for this is:
While there are still elements in the ArrayList {
Store the first element
for each element in the arraylist {
if it equals the stored element {
matchFound++
}
if matchFound = number of attendees {
return
}
}
for each matchFound {
remove the stored element from the ArrayList
}
}
Actual Code:
// While there are still elements in allResponses
while (x < allResponses.size()) {
// Store the first meeting response in list
String[] compare = allResponses.get(0);
int matchFound = 0;
for (int i=0; i<allResponses.size(); i++) {
if (compare.equals(allResponses.get(i))) {
matchFound++;
}
// We have an agreement between all attendees
if (matchFound == attendees.length) { return;
}
}
for (int p=0; p<matchFound; p++) {
allResponses.remove(compare);
}
}
I find this works until the second element is compared to the stored element. The first if statement inside the for loop doesn't think that the second element in the ArrayList is equal to the first?! Not sure what to do. I have tried toString(), converting to array before comparisons, == and .equals().
Any help is much appreciated!
To add some context to the dilemma, the strings are date/times (in the format DD/MM/YYYY-HHMM) for which certain attendees are available for a meeting. For example, if two attendees are available on May 3rd 2004 at 9am, the ArrayList will be [03/05/2004-0900,03/05/2004-0900]. I want to be able to check through the arraylist and find out if the same string appears the same amount of times as there are attendees (meaning all attendees are free at that time).
My pseudo code for this is:
While there are still elements in the ArrayList {
Store the first element
for each element in the arraylist {
if it equals the stored element {
matchFound++
}
if matchFound = number of attendees {
return
}
}
for each matchFound {
remove the stored element from the ArrayList
}
}
Actual Code:
// While there are still elements in allResponses
while (x < allResponses.size()) {
// Store the first meeting response in list
String[] compare = allResponses.get(0);
int matchFound = 0;
for (int i=0; i<allResponses.size(); i++) {
if (compare.equals(allResponses.get(i))) {
matchFound++;
}
// We have an agreement between all attendees
if (matchFound == attendees.length) { return;
}
}
for (int p=0; p<matchFound; p++) {
allResponses.remove(compare);
}
}
I find this works until the second element is compared to the stored element. The first if statement inside the for loop doesn't think that the second element in the ArrayList is equal to the first?! Not sure what to do. I have tried toString(), converting to array before comparisons, == and .equals().
Any help is much appreciated!