Created
November 23, 2018 18:14
-
-
Save rodrigovilar/6c04416b11a7c4c5ec64fab79ba5b382 to your computer and use it in GitHub Desktop.
Material da Aula 1
This file contains hidden or 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
import java.util.Arrays; | |
public class Aula1 { | |
static int [ ] items = {12, 3, 43, 55, 25, 1, 6, 2}; | |
public static void main(String[] args) { | |
Aula1 aula1 = new Aula1(); | |
System.out.println(aula1.maiorInteiro(items)); | |
System.out.println(aula1.maiorInteiroForeach(items)); | |
System.out.println(aula1.menorInteiro(items)); | |
System.out.println(Arrays.toString(aula1.trocarMenor(items))); | |
} | |
public int menorInteiro(int [ ] items) { | |
int menor = Integer.MAX_VALUE; | |
for (int i = 0; i < items.length; i++) { | |
if (items[i] < menor) { | |
menor = items[i]; | |
} | |
} | |
return menor; | |
} | |
public int[] trocarMenor(int [ ] items) { | |
int menorValor = Integer.MAX_VALUE; | |
int menorPosicao = 0; | |
for (int i = 0; i < items.length; i++) { | |
if (items[i] < menorValor) { | |
menorValor = items[i]; | |
menorPosicao = i; | |
} | |
} | |
int temp = items[0]; | |
items[0] = items[menorPosicao]; | |
items[menorPosicao] = temp; | |
return items; | |
} | |
public int maiorInteiro(int [ ] items) { | |
int maior = Integer.MIN_VALUE; | |
for (int i = 0; i < items.length; i++) { | |
if (items[i] > maior) { | |
maior = items[i]; | |
} | |
} | |
return maior; | |
} | |
public int maiorInteiroForeach(int [ ] items) { | |
int maior = Integer.MIN_VALUE; | |
for (int item : items) { | |
if (item > maior) { | |
maior = item; | |
} | |
} | |
return maior; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment