将数组中的数组中的对象元素添加到字符串数组中

时间:2013-02-12 19:44:13

标签: java arrays string object loops

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

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(菜单)和食谱(配料)的对象数组。

现在运行时,这段代码会让我进入NullPointerException,因为尝试对字符串测试空值,但是如何通过配方检查它,如果它没有找到任何内容,它会移动到菜单中的下一个配方,如果它确实如此,它只是添加它,但继续检查直到完成。我尝试添加“if”语句检查空值而不是空值,但它变得复杂,但它仍然没有让我的程序返回检查其余的数组。我知道第一个“if”可以保留,因为,如果我在Menu中检查的点是null,其余部分必须为null,所以没有必要再往前走了。但是,我如何检查Ingredients数组,找到一些东西,添加它,然后返回筛选菜单中的配方?是否有可能在内部循环中添加一个if来检查null,如果是,只需返回外部循环?

2 个答案:

答案 0 :(得分:0)

如果条件如下更新

第一条条件:

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

第二条条件:

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

答案 1 :(得分:0)

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

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;
}