0

I have an app in which contain ArrayList of some String let suppose a,b,c,d and i have a String called pack. What i have to do is that i have to check whether pack is already exist in arraylist or not if not exist then i have to add it in arraylist. How do i do that

code:-

ArrayList<CBlackList> appsListDataSet;
 public void setAppsDetails(String pack) {
    if (appsListDataSet != null && appsListDataSet.size() > 0) {
        for (int i = 0; i < appsListDataSet.size(); i++) {
            CAppsModel appsModel = appsListDataSet.get(i);
            if (pack.equalsIgnoreCase(appsModel.getPackageName())){
                //Not to know what to do here.
            }
        }
    } 
}
Niraj
  • 7
  • 1
  • 7

2 Answers2

1

Use the contains method in an arrayList. If your arrayList contains only String and you want to check for String value itself, then just do

boolean isPackPresent = appsListDataSet.contains(pack)

this will work if your arraylist has the same datatype as pack and is not a custom object.

Kapil G
  • 4,081
  • 2
  • 20
  • 32
1

Try Some thing like this

if(appsListDataSet.contains("your String"))
{
 ///Action to be occur
}
Anil
  • 1,605
  • 1
  • 14
  • 24