lundi 9 décembre 2019

Adding multiple objects to ArrayList that match a selected string

What I am trying to do: Fill the namesArray with all the objects that match a certain criteria.

The Activity object is made of four strings "category, name, note, time"

The getCategory(activityArray) returns a string which is the selected category, I'm trying to sort the objects where their category matches the selected category into the new namesArray

So this namesArray will end up containing only Activity objects with selected category.

This is my code:

 public static void viewActivity(ArrayList<Activity> activityArray) {
      String categ = getCategory(activityArray);
      String holder;
      ArrayList<Activity> namesArray = new ArrayList<Activity>();
      for(Activity obj : activityArray) {
         holder = obj.getCategory();
         if(holder == categ) {
            System.out.println(obj.getName());
            namesArray.add(obj);
         }
      }
}

When I run the debugger, first iteration works, and the first object where it's category matches the selected category is added to the namesArray. But then the if statement just seems to stop working, the holder String does not change and stays the same.

So how do I get my method to add every matching object to the namesArray instead of only the first one?

Thanks

Aucun commentaire:

Enregistrer un commentaire