2013-02-12 77 views
1

我有一個假設要遍歷2個Ojbects數組的方法,第一個是大小爲50的菜單,其中包含Recipes,它最多可容納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)的對象數組。

現在,當運行這段代碼時,我得到了一個N​​ullPointerException異常,因爲我試圖對字符串進行空值測試,但是如何通過配方檢查它,如果找不到任何東西,它就會轉到菜單中的下一個配方,如果是這樣,它只是增加它,但繼續檢查直到完成。我嘗試添加「if」語句來檢查空值而不是空值,但它變得複雜,並且它仍然不會讓我的程序返回到檢查其餘數組。我知道第一個「if」可以留下,因爲如果我在Menu中檢查的位置是空的,那麼它的其餘部分必須爲空,所以沒有必要繼續前進。但是,如何查看配料列表,找到某種東西,添加它,然後回過頭來篩選配料菜單中的菜單?是否有可能在內部循環內添加一個if來檢查是否爲null,如果是,只需返回到外部循環?

+1

第一個問題:爲什麼你使用數組而不是列表? – 2013-02-12 19:44:50

+0

需要我,不幸 – Sherifftwinkie 2013-02-12 19:46:34

回答

0

更新,如果條件如下

第一if條件:

如果(菜單[J] ==空||菜單[j]的.Ingredients == NULL ||菜單[j]的.Ingredients。長度== 0)

第二如果條件:

如果(菜單[j]的.Ingredients [K] = NULL & & ingredientName.equal(菜單[j]的.Ingredients [K] .getName() )) 請讓我知道是否有任何問題。

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