I have an String ArraList
that fill out from DB. And an Object ArrayList
that each object contains some String
. Now i want to compare the String ArrayList
with one of the Strings of each object in the Object ArrayList
. If it equals the Object from Object ArrayList
must be remove. Here is my Method
Code :
public ArrayList checkMatches(ArrayList<IceCream> iceCreams, ArrayList<String> blackListPartCodes) { // output is filtered Object ArrayList and two Input,
//one is the Object ArrayList and other is String ArrayList
int i, j;
for(i = 0; i<iceCreams.size(); i++) { // Object ArrayList
IceCream iceCream = iceCreams.get(i);
for(j = 0; j<blackListPartCodes.size(); j++) { //String ArrayList
if ((iceCream.getPartCode().matches(blackListPartCodes.get(j)) || iceCream.getPartCode().equals(blackListPartCodes.get(j)))) {
iceCreams.remove(iceCream);
}
}
}
return iceCreams;
}
Ok,When i use this method, it removes some objects from my Object and decrease the lenght of ArrayList, but does not work correctly. Im i doing something wrong?
I used this code in my app to see if method work fine or not :
Toast.makeText(getApplicationContext(), "Before : " + iceCreams.size(), Toast.LENGTH_LONG).show(); //324
checkMatches(iceCreams, blackListPartCodes);
Toast.makeText(getApplicationContext(), "After : " + iceCreams.size(), Toast.LENGTH_LONG).show(); //200
The first lenght
of iceCreams
is 324 and when method interact the lenght
is 200. I read String ArrayList(blackListPartCodes) from DB, that when i use select Count(myField) from MyTable
it says it has 215 rows. that means if it works correctly shoud be 324-215 that is 109. From the other side i display one of the Strings of each object in ListView
. Via this :
for(int i = 0; i<iceCreams.size(); i++) { // the filtered iceCreams after calling `checkMatches`method.
typeArray.add(iceCreams.get(i).getPartName()); // String ArrayList
typeAdapter.getItemViewType(R.id.listView); //Adapter of the Array
iceCreamTypeList.setAdapter(typeAdapter); //Adapter set to ListView
}
But in view the fields that are in blackListPartCodes
still exist.
I tried to simplyfy your method. It now iterates throw your arrays and places the matches in a new array list. Which is not touching or changing your existing arrays:
public ArrayList checkMatches(ArrayList<IceCream> iceCreams, ArrayList<String> blackListPartCodes) {
ArrayList<IceCream> matches = new ArrayList();
for (IceCream iceCream : iceCreams) {
for (String blackListPartCode : blackListPartCodes) {
if (blackListPartCode.equals(iceCream.getPartCode())
|| blackListPartCode.matches(iceCream.getPartCode())) {
matches.add(iceCream);
}
}
}
return matches;
}
If i == 0 and it finds a match, it removes the item with index 0 in iceCream. All the other items in the list decrease their index by one. So in this example, the item which originally had index 1 will not be checked as its new index is 0 and i == 1.
Instead, keep track of the matching items and remove them after iterating through the list.
Simple solution to compare objects of same class , follow the steps:
1. Open your IceCream.java file.
2. Add following lines of code :
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (getClass() != o.getClass()) {
return false;
}
final IceCream other = (IceCream) o;
return this.name == other.name;
}
3. Now compare your IceCream class object with another IceCream class object using equals(obj) method.
Hope! it works. :D
I Finally find out what the problem is. I should identify the iceCream object in inner loop. In this way :
for(i = 0; i<iceCreams.size(); i++) {
for(j = 0; j<blackListPartCodes.size(); j++) {
IceCream iceCream = iceCreams.get(i);
And not this way :
for(i = 0; i<iceCreams.size(); i++) { // Object ArrayList
IceCream iceCream = iceCreams.get(i);
for(j = 0; j<blackListPartCodes.size(); j++) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.