I have a problem with re defining columns and rows in an array only once and when I use it again I leave it the same

0
public static void main (String args[])
{  int f = Integer.parseInt(JOptionPane.showInputDialog("Cuantas filas"));
   int c = Integer.parseInt(JOptionPane.showInputDialog("Cuantascolumnas"));
   int [][]mat = new int[f][c];int cont = 0;
    while(cont < 2)
    {   int [][]columR = redef_colum(mat);
        mat = columR;
       /* int [][]filaR = redef_fila(mat);
        mat = filaR;*/
        cont++;
    }
    String result = "";
    for(int x = 0; x < mat.length; x++)
    {   for(int y = 0; y< mat[x].length; y++)
        {  result += mat[x][y] + " "; } result += "\n";};
    JOptionPane.showMessageDialog(null,result);    } 

public static int[][] redef_colum(int m[][])
{  
    int[][] redef_colum = new int [m.length][m.length + 1];        
    for(int f = 0; f < redef_colum.length; f++)
    {   for(int c = 0; c < redef_colum[f].length; c++)
        {  if(c < redef_colum.length)       
           { redef_colum[f][c] = m[f][c];}
           else
           {redef_colum[f][c] = 0;}                
        }            
    }  
    return redef_colum;
}
public static int[][] redef_fila(int m[][])
{  
    int[][] redef_fila = new int [m.length +1][m.length];       
    for(int f = 0; f < redef_fila.length; f++)
    { 
        if(f < redef_fila[f].length) 
        { for(int c = 0; c < redef_fila[f].length; c++)
            { redef_fila[f][c] = m[f][c];}
        } 
        else{
            for(int c = 0; c < redef_fila[f].length; c++)
            { redef_fila[f][c] = 0; }
        }       
   } 
   return redef_fila;
} 
    
asked by Alejandro Cast 13.08.2017 в 22:42
source

1 answer

0

1 - Reviewing the code that you place, I find the first error is on the line

int[][] redef_colum = new int[m.length][m.length + 1];

This is why you are defining the redef_colum matrix with only the number of rows; this line should be

int[][] redef_colum = new int[m.length][m[0].length + 1];

2 - This part of the copying cycle is not necessary

if (c < redef_colum.length) {
   redef_colum[f][c] = m[f][c];
} else {
   redef_colum[f][c] = 0;
}

because a) when you define an integer array is by default filled with the value zero 0 and b) You are always going to copy to a larger matrix so you only have to go through the original matrix (matrix m ) and pass it to the destination matrix (matrix redef_colum ).

for (int f = 0; f < m.length; f++) {
     for (int c = 0; c < m[f].length; c++) {
          redef_colum[f][c] = m[f][c];
     }
 }

Now, this is valid if and only if, the origin matrix has data, because if it is empty (the whole matrix is zero) it is not required to do this copying.

3 - You must make the same changes in the public static int[][] redef_fila(int m[][])

method

4 - To traverse the matrix and store its values in a single String variable, it is preferable that you use a StringBuilder .

StringBuilder result = new StringBuilder();
for (int x = 0; x < mat.length; x++) {
    for (int y = 0; y < mat[x].length; y++) {
         result.append(mat[x][y]);
         result.append(" ");
    }
    result.append("\n") ;
}
JOptionPane.showMessageDialog(null, result.toString());
    
answered by 14.08.2017 в 06:32