Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 18, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/caa6c6f09e886ae28840cab7ae238065 to your computer and use it in GitHub Desktop.
Save parzibyte/caa6c6f09e886ae28840cab7ae238065 to your computer and use it in GitHub Desktop.
// 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