2015-07-21 31 views
1

我想檢查任何矩陣中每行的總和是否相等。我應該如何重寫這個以避免NPE?如何避免在我的矩陣求和中獲取NullPointerException?

我可以把它的「正常」的矩陣狀int[][] a = {{1,2,3}, {4,5,6}}工作,但我希望它甚至工作與空和空矩陣測試時。

public static boolean allRowSumsEqual(int[][] m) { 
    boolean a = false; 
    int x = 0; 
    int total = rowSum(m[0]); 

    for (int i = 0; i < m.length; i++) { 
    for (int j = 0; j < m[i].length; j++) { 
     x += m[i][j]; 
    } 
    if (x != total) { 
     a = false; 
     break; 
    } else { 
     x = 0; 
     a = true; 
    } 
    } 
    return a; 
} 

public static int rowSum(int[] v) { 
    int vSum = 0; 
    for (int i = 0; i < v.length; i++) { 
    vSum += v[i]; 
    } 
    return vSum; 
} 
+2

您如何使用'if'語句來檢查矩陣是否爲空? –

回答

1

你需要,如果你想檢查「空」參數來定義一個結果或異常。如果null有效,則返回true,否則返回false

if(m == null) return true; 

空或一個內襯矩陣可以返回true所有的時間,並且不需要任何計算:

if(m.length < 2) return true; 

線測試更簡單,我想:

// expect a positiv result 
boolean result = true; 
// calculate first line 
int firstLine = rowSum(m[0]); 

// loop remaining lines 
for (int i = 1 ; i < m.length ; i++){ 
    // compare first line with current line 
    if (firstLine != rowSum(m[i])) 
    { 
     // not equal -> change result 
     result = false; 
     // break loop 
     break; 
    } 
} 
return result; 
2

正如你所說,這對大多數矩陣都有效。有幾個地方我會檢查空,見下文修改後的代碼:

public static boolean allRowSumsEqual(int[][] m){ 
    if(m == null) return true; 
    if(m.length == 0) return true; 

    boolean a = false; 
    int x = 0; 
    int total = rowSum(m[0]); 

    for (int i = 1; i < m.length; i++){ 
     // You can use your own function instead of the inner for loop 
     x = rowSum(m[i]); 
     if (x != total) { 
      a = false; 
      break; 
     } else { 
      x = 0; 
      a = true; 
     } 
    } 
    return a; 
} 

public static int rowSum(int[] v){ 
    int vSum = 0; 

    // Assume a null row has sum 0 
    if(v == null) return 0; 

    for (int i = 0 ; i < v.length ; i++){ 
     vSum += v[i]; 
    } 
    return vSum; 
} 
0
public class Snippet { 
    public static boolean allRowSumsEqual(int[][] m) { 
     if (null == m || 0 == m.length) 
      return true; 
     boolean a = false; 
     int x = 0; 
     int total = 0; 
     if (null != m[0]) 
      total = rowSum(m[0]); 

     for (int i = 1; i < m.length; i++) { 
      if (null != m[i]) { 
       for (int j = 0; j < m[i].length; j++) { 
        x += m[i][j]; 
       } 
      } else 
       x = 0; 
      if (x != total) { 
       a = false; 
       break; 
      } else { 
       x = 0; 
       a = true; 
      } 

     } 
     return a; 
    } 

    public static int rowSum(int[] v) { 
     int vSum = 0; 
     for (int i = 0; i < v.length; i++) { 
      vSum += v[i]; 
     } 
     return vSum; 
    } 

    public static void main(String[] args) { 
     int[][] a = { { 1, 2, 3 }, { 3, 2, 1 }, null }; 
     System.out.println(allRowSumsEqual(a)); 
     int[][] b = { null, null, null }; 
     System.out.println(allRowSumsEqual(b)); 
    } 
} 
0

什麼你覺得這個解決方案:

public static boolean allRowSumsEqual(int[][] m) { 

    if(m == null) { return false; } 

    int sum = 0; 

    for(int i = 0; i < m.length; i++) { 

     int temp = 0; 

     if(m[i] == null) { continue; } 

     for(int j = 0; j < m[i].length; j++) { 
      temp += m[i][j]; 
     } 

     if(i == 0) { //is the first row 
      sum = temp; 
     } 
     else if(sum != temp) { 
      return false; 
     } 
    } 

    return true; 
} 
相關問題