Está en la página 1de 26

Diseño y Programación

Orientados a Objetos
Conferencia # 5:
Arreglos multidimensionales

Facultad Ing. Informática


Contenido
• Arreglos bidimensionales
• Arreglos bidimensionales con
ArrayList
• Ejemplo
Bibliografía
Bruce Eckel Thinking in Java. Tercera
Edición. 2002.
• Cap. 1, subepígrafe Special case:
primitive types.
• Cap. 4, subepígrafe Multidimensional
arrays.
¿Cuál estructura de datos
usar para almacenar:?
• Nota de n 0 12 3 … n
estudiantes en 5 5 3 4 …
1 asignatura 0 12 3 … n
• Nota de n
0 5 5 3 4 …
estudiantes en
1 3 5 2 3 …
m asignaturas
… . . . . …
m 4 4 4 4 …
Arreglo
0 1… j … n
bidimensional
0 5 5 .. 4 …
1 3 5 .. 3 …
… . . .. . Notas

Asignaturas m*n
i 4 5 .. 4 … notas
… . . .. . …
m
4 4 .. 4 …
Nota en la
asignatura i del
estudiante j Estudiantes
Arreglo multidimensional en
Java
• Un arreglo de arreglos
• Sus elementos son arreglos
• Cada nueva dimensión se
comporta como una colección de
otros arreglos internos
• Un arreglo de referencias a otros
arreglos
• Chequeo de rango
Arreglo multidimensional en
Java
Arreglo multidimensional en
Java
Arreglo multidimensional en
int[][] a1; Java
int[][] a2 = {{0, 1, 2}, {3, 4}};
int[][] a3 = {{0, 1, 2}, {}};

/* lo contrario no es válido */
int[][] a4 = new int [5][3];
int[][] a5 = new int [2][];
/* lo contrario no es válido */
/* NO ES VÁLIDO */
int[][] a6 = new int [][];
Arreglo multidimensional en
Java
• int[][] m; stack heap
m
• m = new int[3][2];

m 0 0
m[0]
m[1] 0 0
stack m[2] 0 0 heap
Arreglo multidimensional en
Java stack heap
• int[][] m; m
• m = new int[3][]; m[0] = new int[2];
m[2] = new int[3];
m m[0] null 0 0
m[1] null
0 0 0
stack m[2] null heap
Arreglo multidimensional en
Java
m m[0] null 0 0
m[1] null
0 0 0
stack m[2] null heap

m.length 3 m[0].length  2
m[2].length  3
Arreglo multidimensional en
Java
m m[0] null 0 0
3
m[1] null
0 1
0 0
stack m[2] null heap

m[0][0] = 3;
m[2][1] = 1;
¿Fila? ¿Columna?
m m[0] null 3 0
m[1] null
0 1 0
stack m[2] null heap

m null
3 0
0 1
stack 0 heap
Inicialización
int[][] m2 = { {1, 2},{4, 5, 6} };

m 1 2
m[0] null
null
m[1] 4 5 6
stack heap
Arreglos
bidimensionales
int [][] a5 = new int[2][];

for(int i = 0; i < a5.length; i++)


a5[i] = new int[2];
 
for(int i = 0; i < a5.length; i++)
a5[i] = new int[i + 1];
Arreglos
bidimensionales

String[][] months=
{"January", "31"},
{"February", "28"},
{"March", "31"},
{"April", "30"},
{"May", "31"},
{"June", "30"},
{"July", "31"},
{"August", "31"},
{"September", "30"},
{"October", "31"},
{"November", "30"},
{"December", "31"},
};
Clases wrapper
a7 = new Integer[3][4];
for(int i = 0; i < a7.length; i++)
for(int j = 0; j < a7[i].length; j++)
a7[i][j] = new Integer(i * j);
 
for (int i = 0; i < a7.length; i++)
for (int j= 0; j < a7[i].length; j++)
System.out.println("a7[" + i +"][" + j + "] = "
+ a7[i][j].intValue());
Clases wrapper

Integer[][] a8 = {
{ new Integer(1), new Integer(2)},
{ new Integer(3), new Integer(4)},
{ new Integer(5), new Integer(6)},
};
Usando instancias
Rectangle[][] rectanglesTable = new Rectangle[3][];

for(int i = 0; i < rectanglesTable.length; i++) {


rectanglesTable [i] = new Rectangle[2];
for(int j = 0; j < rectanglesTable[i].length; j++)
rectanglesTable[i][j] = new Rectangle(i+j,i+j);
}
Usando ArrayList<E>
ArrayList<ArrayList<Integer>> valores =
new ArrayList<ArrayList<Integer>>(2);
for (int i = 0; i < 3; i++) {
valores.add(new ArrayList<Integer>(2));
valores.get(i).add(
new Integer(2* (i + 1)));
valores.get(i).add(
new Integer(3* (i + 1)));
}
Usando ArrayList<E>
for (int i = 0; i < valores.size(); i++)
for (int j = 0; j < valores.
get(i).size(); j++)
System.out.println(
“valores[" + i +"][" + j + "] = " +
valores.get(i).get(j));
Ejemplo
Cantidad de horas trabajadas cada día
laborable de cada mes por un obrero

  ene feb … dic

1 8 8  …  6.2
2 3 8  …  5
… … … … …

24 5.7 8  …  8
Ejemplo
¿Cómo obtener:

el promedio de horas que trabajó


diariamente en un mes dado?
• el o los meses en que trabajó menor
cantidad de horas?
• la cantidad total de horas trabajadas en
el año?
• la cantidad de días que no asistió al
trabajo en un mes?
Conclusiones

arreglo bidimensional ? matriz

Arreglo tridimensional
Estudio Independiente
1. Implementar las funcionalidades para el ejemplo
de las horas trabajadas por un obrero.
2. Preparar los incisos para el ejercicio de la clase
práctica.

También podría gustarte