I understand that what is sought is to print as many characters as rows have been printed. To achieve this I would do so by modifying the limits of the loops in the following way:
int columnas;
int filas;
int i;
int j;
//System.out.println("Escribe el numero de Columnas");
//columnas =pedir.nextInt();
//System.out.println("Escribe el numero de Columnas");
//filas =pedir.nextInt();
System.out.println("4 Columnas y 4 filas");
columnas = filas = 4;
int numPrimos[][] = new int [columnas][filas];
for(i = 0;i<numPrimos.length;i++){
//int A ; <- Linea innecesaria
//for(j = 0;j <numPrimos.length;j++){
for(j = 0;(j <= i && j<numPrimos[i].length);j++){ // De esta forma rellenamos los elementos que nos interesan únicamente. Esto es opcional pero según mi opinión si no se va a trabajar con un objeto mejor no instanciarlo para optimizar el proceso.
numPrimos[i][j]=j+1; // Se añade 1 para que lo que se muestre en pantalla empieze en 1 y no en 0.
//A = numPrimos[i][j]; <- Linea innecesaria
}
}
for(i = 0;i<numPrimos.length;i++){
// Esta es la línea de código que cambiaría para conseguir dicho resultado
//for(j = 0;j <numPrimos.length;j++){
for(j = 0;(j <= i && j<numPrimos[i].length);j++){ // Así conseguimos que se impriman solo los carácteres que necesitamos sin llegar al limite numPrimos[i].length
System.out.print(numPrimos[i][j]);
}System.out.print("\n");
}
I tell you about the changes. First the first loop has been changed, so that it only stores in the matrix the values with which it is going to work in this process. Before, the matrix was as follows:
[0][0] = 1 [1][0] = 1 [2][0] = 1 [3][0] = 1
[0][1] = 2 [1][1] = 2 [2][1] = 2 [3][1] = 2
[0][2] = 3 [1][2] = 3 [2][2] = 3 [3][2] = 3
[0][3] = 4 [1][3] = 4 [2][3] = 4 [3][3] = 4
After the change in the first loop, the matrix remains as follows:
[0][0] = 1 [1][0] = 1 [2][0] = 1 [3][0] = 1
[0][1] unde. [1][1] = 2 [2][1] = 2 [3][1] = 2
[0][2] unde. [1][2] unde. [2][2] = 3 [3][2] = 3
[0][3] unde. [1][3] unde. [2][3] unde. [3][3] = 4
On the other hand, the change in the second loop prevents that there are more laps than necessary when it comes to show. As you may have noticed, it is the same solution in both loops, to prevent it from turning more than you really need. In an example of 4 lines there is hardly any difference, but if the same code executes millions of records, the difference between doing it efficiently or not is much greater.
So that you can do the test, I add the results of having done it with the performance improvement and without it for a 400x400 matrix.
Sin mejora - 834ms.
Con mejora - 276ms.
I hope it helps you to take into account all the details when you look for a solution, a greeting.