EJEMPLOS DE MATRICES EN JAVA
Programa que genera una matriz de dos dimensiones con letras del alfabeto, mostrndola posteriormente en
pantalla.
public static void main(String[] args) {
final int FILAS = 3;
final int COLUMNAS = 5;
char letras[][] = new char[FILAS][COLUMNAS];
char letraQueToca = 'A';
//Carga las letras en la matriz
for(int f=0; f<FILAS; f++) {
for(int c=0; c<COLUMNAS; c++) {
letras[f][c] = letraQueToca;
letraQueToca++;
}
}
//Mostrar en pantalla la matriz
for(int f=0; f<FILAS; f++) {
for(int c=0; c<COLUMNAS; c++) {
System.out.print(letras[f][c]+" ");
}
System.out.println();
}
}
Programa que genera una matriz representando un tringulo invertido alineado a la derecha realizado con asteriscos,
cuyo tamao ser especificado por el usuario. La matriz ser mostrada en pantalla finalmente. Por ejemplo, si se
indica el tamao 5 deber aparecer:
*****
****
***
**
*
public static void main(String[] args) {
final int TAM = 5;
char triangulo[][] = new char[TAM][TAM];
//Cargar matriz con el dibujo
for(int f=0; f<TAM; f++) {
//Bucle para mostrar espacios
for(int c=0; c<f; c++)
triangulo[f][c] = ' ';
//Bucle para mostrar asteriscos
for(int c=f; c<TAM; c++)
triangulo[f][c] = '*';
}
//Mostrar matriz en pantalla
for(int f=0; f<TAM; f++) {
for(int c=0; c<TAM; c++)
System.out.print(triangulo[f][c]);
System.out.println();
}
}
Tomado de: http://javiergarbedo.es/index.php/31-arrays/127-ejemplos-de-matrices-en-java