M×Nの行列について、要素が0であれば、その行と列の全てを0にするようなアルゴリズムを書こうとしています。
以下が実際に作ってみたものなのですが、題名の通り、Arrat¥yIndexOutOfBoundeExceptionが出るのですが、なぜ出るのかがわかりません。
お力を貸していただけると幸いです。
Java
1public class Main { 2 3 public static void main(String[] args) { 4 int[][] matrix = new int[3][4]; 5 for(int[] x: matrix){ 6 java.util.Arrays.fill(x, 3); 7 } 8 matrix[1][1]=0; 9 System.out.println(Arrays.deepToString(matrix)); 10 makeRawAndLine0(matrix,3,4); 11 System.out.println(Arrays.deepToString(matrix)); 12 } 13 14 public static void makeRawAndLine0(int[][] matrix, int m, int n) { 15 for(int i=0;i <m; i++) { 16 for(int j = 0; i <n; i++) { 17 if(matrix[i][j]==0){ 18 executingMaking0(matrix,i,j,m,n ); 19 } 20 } 21 } 22 } 23 24 public static void executingMaking0(int[][] matrix,int i, int j, int m, int n) { 25 for(int k =0; k<m; k++) { 26 matrix[k][j] =0; 27 } 28 for(int l = 0; l<n; l++) { 29 matrix[i][l]=0; 30 } 31 }
回答お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。