vetores e matrizes em java - wordpress.com...vetores e matrizes em java aula 06 programação...
Post on 25-Aug-2020
15 Views
Preview:
TRANSCRIPT
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
▪
»
»
»
»
»
Edmilson Campos (edmilson.campos@ifrn.edu.br) 2
▪
▪
▪
▪
▪
Edmilson Campos (edmilson.campos@ifrn.edu.br) 3
▪
▪
»
»
Edmilson Campos (edmilson.campos@ifrn.edu.br) 4
»
»
»
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 (edmilson.campos@ifrn.edu.br) 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 (edmilson.campos@ifrn.edu.br) 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 (edmilson.campos@ifrn.edu.br) 7
▪
Edmilson Campos (edmilson.campos@ifrn.edu.br) 8
▪
»
▪»
stack
heap
int[] vetorVazio;
int[] vetor = new int[4];
vetorVazio
0 0 0 0 @
vetor
Edmilson Campos (edmilson.campos@ifrn.edu.br) 9
▪
Edmilson Campos (edmilson.campos@ifrn.edu.br) 10
▪
▪
▪»
»
▪int[] vetor = new int[10]; vetor.length;
int[][] matriz = new int[2][3]; matriz.length; matriz[0].length;
Edmilson Campos (edmilson.campos@ifrn.edu.br) 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 (edmilson.campos@ifrn.edu.br) 14
▪
▪
»int[] pins = { 9, 3, 7, 2 }; for (int pin in pins) { System.out.println(pin); }
Edmilson Campos (edmilson.campos@ifrn.edu.br) 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 (edmilson.campos@ifrn.edu.br) 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(""); } }
1.
2.
3.
Edmilson Campos (edmilson.campos@ifrn.edu.br) 19
4.
5.
6.
Edmilson Campos (edmilson.campos@ifrn.edu.br) 20
Edmilson Campos (edmilson.campos@ifrn.edu.br) 21
▪
▪
▪
▪
▪
top related