vetores e matrizes em java - wordpress.com...vetores e matrizes em java aula 06 programação...
TRANSCRIPT
![Page 1: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/1.jpg)
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
![Page 5: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/5.jpg)
»
»
»
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
![Page 6: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/6.jpg)
▪
»
<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
![Page 7: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/7.jpg)
▪
▪
▪
»
»
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
![Page 9: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/9.jpg)
▪
»
▪»
stack
heap
int[] vetorVazio;
int[] vetor = new int[4];
vetorVazio
0 0 0 0 @
vetor
Edmilson Campos ([email protected]) 9
![Page 11: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/11.jpg)
▪
▪
▪»
»
▪int[] vetor = new int[10]; vetor.length;
int[][] matriz = new int[2][3]; matriz.length; matriz[0].length;
Edmilson Campos ([email protected]) 11
![Page 12: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/12.jpg)
▪
▪
»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
![Page 13: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/13.jpg)
▪
▪
»int[] pins = { 9, 3, 7, 2 }; for (int pin in pins) { System.out.println(pin); }
Edmilson Campos ([email protected]) 15
![Page 14: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/14.jpg)
▪
»
»
»
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
![Page 15: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/15.jpg)
▪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]); } } }
![Page 16: Vetores e Matrizes em Java - WordPress.com...Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos » » » » » 2 Edmilson Campos (edmilson.campos@ifrn.edu.br) 3](https://reader035.vdocuments.pub/reader035/viewer/2022062403/5fd9c5feb633f85f203b7b60/html5/thumbnails/16.jpg)
▪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(""); } }