1

Hello again stackoverflow, I have a question concerning List of Objects.

I have tried out writing some things, but can't find it though. How do I find an Object in a list of objects, when these objects are lists of its own?

this is what i have so far:

Recipe is a List of: {modification[], ingredients[], recipeBookName, recipeName}

public void removeFromBook(Recipe recipeName) {
    recipes = getRecipes();
    emptyRecipe = getEmptyPage(recipeBookName);

Now, I want to replace a Recipe which has a recipeName, by the emptyRecipe. I think it will be something like:

for(r in recipes) {
    if(r.recipeName == recipeName) {
        list.replace(Recipe, emptyRecipe)
    } else {

    }
}

any ideas? :)

here's my constructor for the Recipe class:

    public String[] modifications;
public String[] ingredients;
public String recipeName;
public String partOfRecipeBook;

public Recipe(String recipeName, String[] modifications, String[] ingredients, String recipeBookName){
    setRecipeModifications(modifications);
    setRecipeIngredients(ingredients);
    setRecipeName(recipeName);
    setRecipeBookName(recipeBookName);
}
8
  • So you have a List<List<Object>>, and each List<Object> is a Recipe list? Using List<Object> isn't really taking advantage of generics at all. Can you show us a little more code (like how you add objects to the list, what objects you make, etc.) Commented May 15, 2012 at 14:02
  • It seems that you're using the word List not like a java programmer would expect. You don't mean a java.util.List, do you? Commented May 15, 2012 at 14:06
  • What is the type of recipes? Commented May 15, 2012 at 14:06
  • Recipe is a List of: {modification[], ingredients[], recipeBookName, recipeName} that does not sound right. I don't think you should use a list for storing the different things, but define your own class which has dedicated members for the modifications, ingredients and so on Commented May 15, 2012 at 14:08
  • eum, hold on lemme search my code :) Commented May 15, 2012 at 14:08

3 Answers 3

2

Using a List with Objects in it (of which some are arrays) is not the best way it would be better to define a new object Recipe and use that.

public class Recipe {
    private List<Ingredient> ingredients;
    private List<Modification> modifications;
    private String bookName;
    private String book;
}

Then replacing in ingredients is a lot simpler. e.g. give recipe a function like

public void replaceIngredent(Ingredient oldIngredient, Ingredient newIngredient) {
    int index = ingredients.indexOf(oldIngredient);
    if (index != -1) {
        ingredients.remove(index);
        ingredients.add(index, newIngredient);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

True, I should get some more coffee and a break, I'm really messing up my things.. too much coding :) thank you
sorry replaceItem does not exist.
@DavidVandenbroucke, if you do use a class Ingredient instead of a String, then remember to implement an equals function (this is used by indexOf).
This one did the trick eventually :) changed the green V :) Thanks again!
2

Your approach looks fine, except that you should compare strings with equals:

if(r.recipeName.equals(recipeName)) {

Now an even easier way would be to store your recipes in a Map:

Map<String, Recipe> recipes = new HashMap<String, Recipe>();
recipes.put("Pizza with Lobster", new Recipe());

when you want to replace a recipe:

recipes.put("Pizza with Lobster", emptyRecipe);

and the old recipe has been replaced.

Comments

0
 private List<TimeModel> timeModels = new ArrayList<TimeModel>();

then you simply put

  timeModels.set(0, new TimeModel("6:00 PM"));

and after that set yourAdapter.notifyDataSetChanged();

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.