2017-03-20 102 views
-5

我有一個String [] []數組,我想從中創建一個新的鋸齒數組而不爲null。從二維字符串[] []數組中刪除所有零

String[][] noZero = new String[WCCount][]; 
String spot = "0"; 
      for(int i = 0; i < WCCount; i++) { 
       noZero[i] = new String[countArray[i]]; 
       for(int j = 0; j < countArray[i]; j++) { 
        if(!ny[i][j].equals(spot)) 
        noZero[i][j] = String.valueOf(j+1); 
        else 
        continue; 
        } 
       } 

的問題是,而不是僅複製值給noZero陣列時NY [i] [j]不等於「0」,它拷貝零到noZero陣列時NY [i] [j ]等於「0」。

它應該跳轉到下一次迭代,如果ny [i] [j]等於「0」,則不執行任何操作。

希望有人能幫助:-)

+3

你甚至嘗試什麼嗎? – dabadaba

+3

爲什麼你使用字符串時,你的值都是數字? –

+0

也許他正在從System.in或其他東西讀取它,並沒有轉換爲數字。如果是這種情況,nlk,您可以使用掃描儀讀取並使用nextDouble()來獲取您的值。 – Edd

回答

0

這裏有一個例子:

for (int i = 0; i < array.length; i++) { 
for (int j = 0; j < array[i].length; j++) { 
    if ("0".equals(array[i][j])) { 
     // remove 
     array[i][j] = null; 
     // or 
     // array[i][j]=""; 
    } 
}}