I have a problem with the contains() method which returns false even though the ArrayList contains the given Object.
My code is following:
String [] transaction = dbConnection.getPendingTransaction(username);
if (!onlineConnection.getSentTransactionRequests().contains(transaction)) {
onlineConnection.getSentTransactionRequests().add(transaction);
String packet = "RTR" + "_" + transaction[0] + "_" + transaction[2] +
"_" + transaction[3] + "_" + transaction[4];
onlineConnection.send(packet);
}
I have tried Thread.sleep() between iterations, so the ArrayList wasn’t load as eagerly without success.
The
hashCode()andequals()of arrays are a bit broken when it comes to this (it is a long different discussion why).A possible work around is to use
ArrayList<ArrayList<String>>instead ofArrayList<String[]>, theequals()method forArrayListwill be as you expect it to.For example:
Will yield
true, as expected