Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /homepages/4/d506406580/htdocs/web_principal/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340 Matrixes und Arrays – Duermes y me Olvidas
Skip to content
A veces tienes que crear varias variables del mismo tipo para guardar datos parecidos. Por ejemplo, imaginate que tienes la clase huevo y la clase caja de huevos. En cada clase caja de huevos puedes tener 6 huevos. Pero… ¿Y si otra vez se tienen 12 huevos? ¿Vas a tener que modificar la clase o crear una clase abstracta de la que heredan caja6huevos y caja12huevos? Hay una solución para eso: Matrices y arrays. Fijate en esta linea de código:
int [] numeros = new int[6];
Con esto hemos creado 6 variables del tipo int, a las que se accede desde el 0 hasta el 5 así:
numeros[0] = 10;
numeros[1] = 20;
…
numeros[5] = 22;
Otra forma de crearlos es así:
int [] numeros = {10,20,30,40,50};
Así hemos creado un array de 5 elementos. Para acceder al primero tendremos que usar el numeros[0].
Para crear un array de un objeto concreto usamos el siguiente comando:
Objeto [] array = new Objeto [10];
array[0] = new Objeto(); // aqui llamamos al constructor
Una matriz es un array de 2 dimensiones. Realmente pueden tener todas las dimensiones que se quieran hacer, pero realmente es algo poco común y que realmente usarás muy poco.
int [][] numeros = new int[10][10];
Así hemos creado una tabla de 10 x 10. Todo lo demás es igual que con el resto de arrays.
Así que ya hemos visto como trabajar con matrices y arrays. ¿Como hacemos lo del ejemplo del inicio? Aprovechando el constructor de la clase:
Huevo [] arrayHuevos;
void CajaHuevos(int cantidad){
arrayHuevos = new Huevo[cantidad];
}
Con esto podemos usar unas veces cajas de 12 huevos, otras de 6, y otras de lo que haga falta.
Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/4/d506406580/htdocs/web_principal/wp-includes/class-wp-comment-query.php on line 399
Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/4/d506406580/htdocs/web_principal/wp-includes/class-wp-comment-query.php on line 399
Leave a Reply