Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação...
Transcript of Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação...
Disciplina:
MsC. Alexandro Vladno
Edmilson Campos
MsC. Fábio Procópio
Esp. Felipe Dantas
MsC. João Maria
MsC. Liviane Melo
Corpo docente:
Vetores e Matrizes em Java
AULA 06
Programação Orientada à Objetos
»
»
»
int vetor1[] = new int[3];
int[] vetor3 = new int[]{1,2,3};
<tipo> vetor[] = new <tipo>[tamanho];
<tipo>[] vetor = new <tipo>[tamanho]; int[] vetor2 = new int[3];
<tipo> vetor[] = {val0, val1, ..., valN-1};
<tipo>[] vetor = {val0, val1, ..., valN-1};
<tipo>[] vetor = new <tipo>[ ] {val0, ..., valN-1};
int[] vetor5 = {1,2,3};
int vetor4[] = {1,2,3};
Edmilson Campos ([email protected]) 5
▪
»
<tipo> matriz[][] = new <tipo>[linhas][colunas]; int m1[][] = new int[2][2];
<tipo>[][] matriz[] = {{val00, val01}, {val10, val11}};
int[][] m2 = new int[2][2];
int[][] m3 = {{1,2},{3,4}};
<tipo>[][] matriz = new <tipo>[linhas][colunas];
Edmilson Campos ([email protected]) 6
▪
▪
▪
»
»
int[] vetor = new int[4] { 1, 2, 3, 4 }; int total = vetor[0] + vetor[1] + vetor[2] + vetor[3];
vetor[0] = vetor[1] = vetor[2] = vetor[3] = 0;
Edmilson Campos ([email protected]) 7
▪
Edmilson Campos ([email protected]) 8
▪
»
▪»
stack
heap
int[] vetorVazio;
int[] vetor = new int[4];
vetorVazio
0 0 0 0 @
vetor
Edmilson Campos ([email protected]) 9
▪
Edmilson Campos ([email protected]) 10
▪
▪
▪»
»
▪int[] vetor = new int[10]; vetor.length;
int[][] matriz = new int[2][3]; matriz.length; matriz[0].length;
Edmilson Campos ([email protected]) 11
▪
▪
»int[] pins = { 9, 3, 7, 2 }; for (int i = 0; i < pins.length; i++) { int pin = pins[i]; System.out.println(pin); }
Edmilson Campos ([email protected]) 14
▪
▪
»int[] pins = { 9, 3, 7, 2 }; for (int pin in pins) { System.out.println(pin); }
Edmilson Campos ([email protected]) 15
▪
»
»
»
for (int i = 0; i < copy.length; i++) { copy[i] = pins[i]; }
int[] pins = { 9, 3, 7, 2 }; int[] copy = new int[pins.length];
System.arraycopy(pins, 0, copy, 0, copy.length);
copy = (int[])pins.clone();
Edmilson Campos ([email protected]) 16
▪public class ExemploVetor { public static void main(String args[]) { java.util.Scanner sc = new java.util.Scanner(System.in); int[] vetor = new int[5]; //Leitura de dados for (int i = 0; i < 5; i++) { System.out.println("Digite:"); vetor[i] = sc.nextInt(); } //Escrita de dados for (int i = 0; i < 5; i++) { System.out.println(vetor[i]); } } }
▪public class ExemploMatriz { public static void main(String args[]) { java.util.Scanner sc = new java.util.Scanner(System.in); int[][] matriz = new int[2][2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.println("Digite:"); matriz[i][j] = sc.nextInt(); } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.print(matriz[i][j] + " "); } } System.out.println(""); } }