Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação...

20
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

Transcript of Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação...

Page 1: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

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 2: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

»

»

»

»

Edmilson Campos ([email protected])2

Page 3: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

Edmilson Campos ([email protected])3

Page 4: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

»

Edmilson Campos ([email protected])4

Page 5: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

»

»

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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

<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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

»

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 8: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

Edmilson Campos ([email protected])8

Page 9: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

▪»

stack heap

int[] vetorVazio;

int[] vetor = new int[4];

vetorVazio

0 0 0 0@

vetor

Edmilson Campos ([email protected])9

Page 10: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

Edmilson Campos ([email protected])10

Page 11: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

▪»

»

▪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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

»

»

»

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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

▪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 dadosfor (int i = 0; i < 5; i++) {

System.out.println("Digite:");vetor[i] = sc.nextInt();

}//Escrita de dadosfor (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 ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

▪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("");

}}

Page 17: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

1.

2.

3.

Edmilson Campos ([email protected])19

Page 18: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

4.

5.

6.

Edmilson Campos ([email protected])20

Page 19: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

7.

8.

9.

Edmilson Campos ([email protected])20

Page 20: Vetores e Matrizes em Java - WordPress.com€¦ · Vetores e Matrizes em Java AULA 06 Programação Orientada à Objetos ... (AULA 06) Arrays Author: Edmilson Keywords: IFRN Created

Edmilson Campos ([email protected])22