2016-11-01 31 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 ..但仍然尝试一下,看看 –