2016-11-01 32 views
0

所以我一直在尋找一個答案這一段時間,我只是發現人們使用ArrayList和一個人做了它,但它是刪除行並在同一時間專欄,我認爲,即時通訊試圖通過提到最後一個提到,但我不知道什麼「繼續;」是指,如何以及其使用時..這是我發現的代碼(我修改變量的名稱,但它仍然有點相同):如何刪除一行或一列在java中的矩陣

public static long [][] removecol(long mat[][],int ren, int col){ 
    int rengre=ren;// row to remove 
    int colre=col;// column to remove 
    long mat2[][]= new long [mat.length-1][mat[0].length-1]; 
    int p=0; 
    for(int i = 0; i <mat.length; ++i) 
    { 
     if (i == rengre) 
      continue; 
     int q = 0; 
     for(int j = 0; j <mat[0].length; ++j) 
     { 
      if (j == colre) 
       continue; 

      mat2[p][q] = mat[i][j]; 
      ++q; 
     } 
     ++p; 
    } 
     return mat2; 
} 

我想有兩種方法,也許分離,一個刪除行和其他刪除列,這樣的事情:

public static long [][] removerow(long mat[][],int ren){ 
    int rengre=ren;//row to remove 
    long mat2[][]= new long [mat.length-1][mat[0].length]; 
    int p=0; 
    for(int i = 0; i <mat.length; ++i) 
    { 
     if (i == rengre) 
      continue; 
     int q = 0; 
     for(int j = 0; j <mat[0].length; ++j) 
     { 
      if (j == colre) 
       continue; 

      mat2[p][q] = mat[i][j]; 
      ++q; 
     } 
     ++p; 
    } 
     return mat2; 
} 

,但我真的不知道該怎麼列和行之間分開這個...我知道你可能我厭倦了關於這個主題的問題,但我根本就不能來以一種方式來做到這一點:c幫助。

+0

continue意味着跳過循環的其餘部分,它開始循環的下一次迭代。如果你的代碼,當任何行或列匹配被刪除時,它不會被添加到最終數組中。然後從該方法返回最終數組。 – Shafiul

回答

0

繼續遇到時,循環中的其餘代碼將被跳過並且會發生下一次循環迭代。 對於防爆:

int [] numbers = {10, 20, 30, 40, 50}; 
    for(int x : numbers) { 
    if(x == 30) { 
     continue;   
     } 
    System.out.print(x);  //when x=30,these will not run; 
    System.out.print("\n"); 
    } 

這裏,當x = 30時,繼續將被執行,你的循環會進入下一個迭代,而不是運行您對繼續code.For更瞭解休息,看現在these examples.

,你列/行刪除problem.Your外環用於​​刪除行和內環用來除去column.If你不想刪除您的欄,然後不使用繼續在內部循環。你的代碼將會是這樣的...

public static long [][] removerow(long mat[][],int ren){ 
    int rengre=ren;//row to remove 
    long mat2[][]= new long [mat.length-1][mat[0].length]; 
    int p=0; 
    for(int i = 0; i <mat.length; ++i) 
    { 
     if (i == rengre) 
      continue; 
     int q = 0; 
     for(int j = 0; j <mat[0].length; ++j) 
     { 
      mat2[p][q] = mat[i][j]; 
      ++q; 
     } 
     ++p; 
    } 
     return mat2; 
} 
+0

oooh謝謝,如果我不想刪除行我將繼續部分更改爲內部循環? – Angelmartin11

+0

well..yes ..但仍然嘗試一下,看看 –