Last active
September 18, 2019 16:01
-
-
Save parzibyte/68ce7c69746c25c5fba2dd95693e9200 to your computer and use it in GitHub Desktop.
Definición de arreglos en Java - https://parzibyte.me/blog/2019/09/18/declarar-arreglos-java-sintaxis-ejemplos/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Main { | |
public static void main(String[] args) { | |
// Definir arreglo de 5 elementos | |
int[] edades = new int[5]; | |
// Establecer elementos | |
edades[0] = 23; | |
edades[1] = 50; | |
edades[2] = 80; | |
edades[3] = 18; | |
edades[4] = 20; | |
// Definir arreglo y sus elementos | |
int[] numeros = new int[] { 1, 2, 3, 4, 5 }; | |
// Arreglo de objetos | |
Mascota[] mascotas = new Mascota[2]; | |
mascotas[0] = new Mascota("Maggie"); | |
mascotas[1] = new Mascota("Panqué"); | |
// Recorremos | |
for (int x = 0; x < edades.length; x++) { | |
System.out.printf("Edades[%d] = %d\n", x, edades[x]); | |
} | |
for (int x = 0; x < numeros.length; x++) { | |
System.out.printf("Numeros[%d] = %d\n", x, numeros[x]); | |
} | |
for (int x = 0; x < mascotas.length; x++) { | |
System.out.printf("Mascotas[%d] = %s\n", x, mascotas[x]); | |
} | |
} | |
} | |
class Mascota { | |
private String nombre; | |
public Mascota(String nombre) { | |
this.nombre = nombre; | |
} | |
@Override | |
public String toString() { | |
return "Mascota{" + "nombre='" + nombre + '\'' + '}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment