package com.codemonkey;
import static java.lang.System.err;
import static java.lang.System.out;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
int[][] matriz = new int[2][2]; //[0,1] [0,1]
int tamFila = matriz.length;//2
int tamColumna = matriz[0].length;//2
out.printf("Fila:%d, Columna:%d\n",tamFila,tamColumna);
out.println("+++++++++++++++++++++++++++");
try{
matriz[0][0] = 12;
matriz[0][1] = 5;
matriz[1][0] = 9;
matriz[1][1] = 22;
//matriz[2][0] = 22;Incorrecto
recorrerMatriz(matriz, "Numero 1");
//[12, 5]
//[9, 22]
}catch(ArrayIndexOutOfBoundsException | NullPointerException e1){
err.println("Mensaje:"+e1.getMessage());
e1.printStackTrace();
}
//No se puede redimensionar un arreglo
//Se crea uno nuevo al hacer esto:
matriz = new int[1][1];// [0] [0]
matriz[0][0] = 12;
recorrerMatriz(matriz,"Numero 2");
//[12]
int[][] matrix = new int[1][1];//[0][0]
try{
matrix[0][0] = 5;
//matrix[0][1] = 3;
//matrix[1][0] = -6;
recorrerMatriz(matrix,"Numero 3");
//[5]
}catch(NullPointerException | ArrayIndexOutOfBoundsException e2){
err.println("Mensaje:"+e2.getMessage());
e2.printStackTrace();
}
int [] miMatrix [] = {{1,4,6},{0,-2},{9},{-5,0,10,3}};
// tam Fila: 4
// tam Columna: 3
// 1 4 6 x
// 0 -2 x x
// 9 x x x
//-5 0 10 3
recorrerMatriz(miMatrix, "Numero 4");
//[1, 4, 6]
//[0, -2]
//[9]
//[-5, 0, 10, 3]
}
static void recorrerMatriz(int[][] matriz, String msg){
out.println("$Matriz:"+msg);
int tamFila = matriz.length;//2
int tamColumna = matriz[0].length;//2
out.printf("Fila:%d, Columna:%d\n",tamFila,tamColumna);
for(int[] mat : matriz){
for(int m : mat){
//Arrays.sort(mat);
}
out.println(Arrays.toString(mat));
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.