【问题标题】:Adding Element of an Object in an Array in an Array to a String Array将数组中的对象的元素添加到字符串数组中
【发布时间】:2013-01-28 05:27:41
【问题描述】:

我有一个假设遍历 2 个 Ojbect 数组的方法,第一个是大小为 50 的菜单,其中包含食谱,其中最多可容纳 10 个称为成分的元素,每个元素最多可容纳 3 个元素,但我只是寻找他们的名字!我想在食谱中获取这些成分元素的匹配名称并将它们添加到我的字符串数组中,然后返回它,这是我的代码...

public class Recipe implements Cloneable
{

    String Name;

    final int INGREDIENT_ARRAY_MAX = 10;

    Ingredient Ingredients[] = new Ingredient[INGREDIENT_ARRAY_MAX];

    public class RecipeBook
    {

        final static int MENU_ARRAY_MAX = 50;

        static Recipe Menu[] = new Recipe[MENU_ARRAY_MAX];

        public static String[] getRecipesByIngredient(String ingredientName)
        {

            String[] targetRecipes = new String[MENU_ARRAY_MAX];

            int counter = 0;

            for (int j = 0; j < Menu.length; j++)
            {

                if (Menu[j] == null)
                {

                    break;

                }

                else
                {

                    for (int k = 0; k < Menu[j].Ingredients.length; k++)
                    {

                        System.out.println(Menu[j].Ingredients[k]);

                        if (Menu[j].Ingredients[k].getName().equals(ingredientName))
                        {

                            targetRecipes[counter] = Menu[j].getName();
                            counter++;

                        }
                    }
                }
            }

            return targetRecipes;

        }
    }
}

现在我知道它不起作用以及为什么,但我不确定解决方案。目前我每个食谱中只有 3 个食谱和 3 个成分!上面的东西仅供参考,是RecipeBook(Menu)和Recipes(Ingredients)的对象数组。

现在,当运行此代码时,我进入 NullPointerException,因为尝试针对字符串测试空值,但我如何让它检查配方,如果它没有找到任何东西,它会转到菜单中的下一个配方,如果确实如此,它只是添加它,但会继续检查直到完成。我尝试添加“if”语句来检查空值而不是空值,但它变得复杂,它仍然没有让我的程序返回检查其余数组。我知道第一个“if”可以保留,因为如果我在 Menu 中签入的位置为空,则其余部分必须为空,因此没有必要再进一步。但是我如何检查成分数组,找到一些东西,添加它,然后返回到菜单中筛选具有该成分的食谱?是否可以在内循环中添加一个 if 来检查是否为 null,如果是,则返回到外循环?

【问题讨论】:

  • 第一个问题:为什么要使用数组而不是列表?
  • 不幸的是,需要我

标签: java arrays string object loops


【解决方案1】:

如果条件如下更新

第一个 if 条件:

如果 (Menu[j] == null || Menu[j].Ingredients == null || Menu[j].Ingredients.length ==0)

第二个 if 条件:

if (Menu[j].Ingredients[k] != null && ingredientsName.equal(Menu[j].Ingredients[k].getName())) 如果有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    我不知道你是如何填充配方数组的,但我可以说你的代码缺少很多空值检查。我会这样(代码未编译/测试):

    public static String[] getRecipesByIngredient(String ingredientName) {
        String[] targetRecipes = null;
        // check input parameter ingredientName against null and do lookup only if it is not null
        if(ingredientName != null) {
            // init the result array and do look up
            targetRecipes = new String[MENU_ARRAY_MAX];
            for (int j = 0; j < Menu.length; j++) {
                // you might run into NPE if Menu[j] or if the array of ingredients in Menu[j] (Menu[j].Ingredients) is null
                if(Menu[j] != null && Menu[j].Ingredients != null) {
                    for (int k = 0; k < Menu[j].Ingredients.length; k++) {
                        // Menu[j].Ingredients[k] may also be null
                        // Menu[j].Ingredients[k].getName() may also be null but no need to check it since
                        // you call equals of the string object ingredientName witch you already checked
                        // and equals(null) is always false in that case
                        if (Menu[j].Ingredients[k] != null && ingredientName.equals(Menu[j].Ingredients[k].getName()) {
                            // here you might want to check Menu[j].getName() against null otherwise you'll have
                            // a null inside your result array (this is some like a land mine) unless you want
                            // to check against null while iterating over you result array
                            if(Menu[j].getName() != null) {
                                targetRecipes[counter++] = Menu[j].getName();
                            }
                        }
                    }
                } // save the else...
            }
        } // else targetRecipes is still null, with witch you may want to say "no result found"
        return targetRecipes;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      相关资源
      最近更新 更多