2017-02-21 44 views
1

不幸的是,我的導師不會讓我爲此作業使用列表。如何限制空值存儲在我的數組中?

我想限制空值存儲在我的數組中。我試圖使用if語句,但它不起作用。

public static void main(String[] args) { 

    System.out.println(messages(3, 10)); 
} 

public static String message(int n) { 

     String message = null; 

     if ((n % 3 != 0) && (n % 5 != 0)){ 
      message = null; 
     } else if((n % 3 == 0) && (n % 5 == 0)) { 
      message = n + ": FizzBuzz"; 
     } else if (n % 3 == 0) { 
      message = n + ": Fizz"; 
     } else if (n % 5 == 0) { 
      message = n + ": Buzz"; 
     } 

     return message; 
} 

public static String[] messages(int start, int end) throws IllegalArgumentException { 
    if (end < start) { 
     throw new IllegalArgumentException(); 

    } else { 

    String[] msgArray = new String[end - start]; 

    int j = 0; 

    for(int i = start; i < end; i++) { 
       String theMsg = message(i); 


/* Where I need help */ 

// I'm trying to use the if statement to restrict 
//all null values from being stored in my msgArray */ 

       if(theMsg != null) { 



       msgArray[j] = theMsg; 

       } 
       j++; 
     } 

    System.out.println(); 

    for(String s: msgArray) { 
     System.out.println(s); 
    } 

    return msgArray; 
     } 
    } 

所需的輸出

3: Fizz 
5: Buzz 
6: Fizz 
9: Fizz 

實際輸出

3: Fizz 
null 
5: Buzz 
6: Fizz 
null 
null 
9: Fizz 
+0

如果你不希望打印'null',然後把周圍的'System.out的一個'if'。 println'來防止'null'被打印。 – ajb

+0

嘿。我不希望它們存儲在數組中。所以當我使用for each循環打印msgArray時,我仍然會看到上面的「Actual Output」。 –

+0

你的代碼看起來很可疑。你在哪裏用msg填充msgArray [j]? – CCC

回答

1

如果創建一個數組,你指定一個大小。該數組然後被創建並填充一個默認值。在這種情況下,對於String s,這是null(它的0對於int[] s)。沒有辦法解決這個問題。您必須從創建數組中指定正確的長度,並且此長度不會更改。所以,你有兩個選擇:

  1. 創建陣列前不知何故計算實際,非空值的數量,然後創建與大小,而不是當前的計算陣列。這涉及到很多在你的邏輯,這樣的變化,
  2. 我會建議只創建從當前數組的新陣列和檢查,看是否有值null你把它放在以前

見下:

// inside your function 
// instead of returning msgArray, create a new array and modify it. 

// get the count of values that aren't null 
int nonNull = 0; 
for (int index = 0; index < msgArray.length; index++) { 
    if (msgArray[index] != null) nonNull++; 
} 

// create the new array because you can't change the size of your other array 
String[] newMsgArray = new String[nonNull]; 

// populate the array 
int newIndex = 0; 
for (int index = 0; index < msgArray.length; index++) { 
    if (msgArray[index] != null) { 
     newMsgArray[newIndex] = msgArray[index]; 
     newIndex++; 
    } 
} 

return newMsgArray; 
+0

嘿,非常感謝! –

0

把J ++內聲明的,如果塊,並嘗試

  if(theMsg != null) { 



      msgArray[j] = "dan"; 
      j++; 
      } 
+0

不夠好。這隻會將'null'移到輸出中的不同位置。 – ajb

1

您正在分配將用空元素初始化的(結束 - 開始)大小數組。然後它的一些索引將被覆蓋。所以null元素仍然存在,無法刪除(Java數組)。解決方案可能是將全部索引複製到另一個數組中,或者先分配正確的數組大小。

0

更優化的方法是使用List s。但由於這是不允許的,那麼你就可以在你的代碼,使這些變化2,

  1. 您可以將您的j++if裏面。
  2. 要刪除陣列中的所有剩餘的null,則可以將msgArray = Arrays.copyOfRange(msgArray, 0, j);放在if語句後面。

輸出

3: Fizz 
5: Buzz 
6: Fizz 
9: Fizz 
[Ljava.lang.String;@6d06d69c 

代碼

import java.util.*; 

public class HelloWorld { 
    public static void main(String[] args) { 
     System.out.println(messages(3, 10)); 
    } 

    public static String message(int n) { 
     String message = null; 

     if ((n % 3 != 0) && (n % 5 != 0)) { 
      message = null; 
     } else if ((n % 3 == 0) && (n % 5 == 0)) { 
      message = n + ": FizzBuzz"; 
     } else if (n % 3 == 0) { 
      message = n + ": Fizz"; 
     } else if (n % 5 == 0) { 
      message = n + ": Buzz"; 
     } 

     return message; 
    } 

    public static String[] messages(int start, int end) throws IllegalArgumentException { 
     if (end < start) { 
      throw new IllegalArgumentException(); 
     } else { 
      String[] msgArray = new String[end - start]; 
      int j = 0; 

      for (int i = start; i < end; i++) { 
       String theMsg = message(i); 


       /* Where I need help */ 

       // I'm trying to use the if statement to restrict 
       //all null values from being stored in my msgArray */ 

       if (theMsg != null) { 
        msgArray[j] = theMsg; 
        j++; //Change #1 
       } 
      } 

      msgArray = Arrays.copyOfRange(msgArray, 0, j); //Change #2 

      System.out.println(); 

      for (String s: msgArray) { 
       System.out.println(s); 
      } 

      return msgArray; 
     } 
    } 
} 
+0

它工作!謝謝。 –