Created
September 18, 2019 16:13
-
-
Save parzibyte/caa6c6f09e886ae28840cab7ae238065 to your computer and use it in GitHub Desktop.
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
// Elemento mayor de arreglo en Java | |
// https://parzibyte.me/ | |
class Main { | |
public static void main(String[] args) { | |
int[] numeros = new int[] { 28, 50, 40, 200, 20, 44, 100, 153 }; | |
// Asumir que el mayor es el primero | |
int indiceDelMayor = 0; | |
// Recorrer arreglo y ver si no es así | |
// (comenzar desde el 1 porque el 0 ya lo tenemos contemplado arriba) | |
for (int x = 1; x < numeros.length; x++) { | |
if (numeros[x] > numeros[indiceDelMayor]) { | |
indiceDelMayor = x; | |
} | |
} | |
// Ahora podemos obtener el mayor usando la posición | |
int mayor = numeros[indiceDelMayor]; | |
System.out.println("El mayor es: " + mayor + " y se encuentra en el índice " + indiceDelMayor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment