1 / 18

Arrays

Capítulo 2. Arrays. Arrays. Introdução Tipo do array Tamanho do array Preenchendo o array Varrendo um array Array com 2 dimensões. Introdução. Permite o armazenamento em memória de diversos valores em uma única variável;

tameka
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Capítulo 2 Arrays

  2. Arrays • Introdução • Tipo do array • Tamanho do array • Preenchendo o array • Varrendo um array • Array com 2 dimensões

  3. Introdução • Permite o armazenamento em memória de diversos valores em uma única variável; • Os valores armazenados em um array devem ser todos do mesmo tipo; • Um array possui o tamanho fixo. Após definir o tamanho de um array, ele não pode ser aumentado, nem diminuído.

  4. Tipo do array • Assim como qualquer outra variável, um array deve sempre possuir um tipo. • O tipo de um array deve ser definido no momento em que o array é declarado. double[] salario; intidade[];

  5. Tamanho do array • O tamanho do array deve ser definido após a sua declaração. salario = newint[5]; 0 1 2 3 4 Variável salario (5 posições)

  6. Preenchendo o array salario[0] = 1250.3; salario[1] = 520.6; salario[2] = 5200; salario[3] = 2500.15; salario[4] = 840.2; 1250.3 520.6 2500.15 0 1 2 5200 3 4 840.2 salario

  7. Declarando arrays double[] salario; floatcomprimento[]; String[] telefone; intidade[];

  8. Declarando e inicializando double[] salario = newdouble[5]; floatcomprimento[] = newfloat[3]; String[] telefone = new String[18]; intidade[] = newint[4];

  9. Declarando, inicializando e preenchendo arrays double[ ] salario = { 1250.3, 520.6, 5200, 2500.15, 840.2 }; String[ ] telefone = { “5689-3214”, “9856-4217”, “7568-0231”, “3276-8241”}; intidade[ ] = { 15, 28, 57, 32, 14, 45, 8, 1, 37, 65};

  10. String[ ] nome = { “Manuel”, “Joaquim”, “Maria”, “Augusto”, “Júlio”, “Silvia” }; for (int i = 0; i < nome.length; i++) { System.out.println(nome[i]);} Forma tradicional (“for” comum) Varrendo um array (forma 1)

  11. String[ ] nome = { “Manuel”, “Joaquim”, “Maria”, “Augusto”, “Júlio”, “Silvia” }; for (String n : nome) {System.out.println(n);} Forma nova a partir do Java 5 (“for each”) Varrendo um array (forma 2)

  12. Array de 2 dimensões (matriz) long[ ][ ] matricula = newlong[3][5]; matricula[0][0] = 12563; matricula[0][1] = 8473; matricula[0][2] = 3693; matricula[0][3] = 8738; matricula[0][4] = 9838 ; matricula[1][0] = 7438; matricula[1][1] = 12987; .... .... matricula[2][4] = 9343; 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 matricula

  13. Array de 2 dimensões (matriz) long[ ][ ] matricula = { { 12563, 8473, 3693, 8738, 9838 }, { 7438, 12987, 0922, 1879, 70987 }, { 20870, 9870, 4788, 9784, 3456} }; 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 matricula

  14. Varrendo uma matriz for (inti = 0; i < matricula.length; i++) { for (intj = 0; j < matricula[i].length; j++) { System.out.println(matricula[i][j]); }}

  15. Varrendo uma matriz for (long[] linha : matricula) { for (longcelula: linha) {System.out.println(celula); }}

  16. Array de objetos Conta[ ]listaConta= new Conta[2];listaConta[0] = new Conta();listaConta[0].setNumero(5003);listaConta[0].setNome("Manuel");listaConta[0].setSaldo(800.0);listaConta[1] = new Conta();listaConta[1].setNumero(5004);listaConta[1].setNome("Joaquim");listaConta[1].setSaldo(650.0);

  17. Array de objetos Conta[ ] listaConta= {new Conta(5003, "Manuel", 800.0) ,new Conta(5004, "Joaquim", 650.0) ,new Conta(5005, "Maria", 1020.0) ,new Conta(5006, "Carlos", 580.5)};

  18. Array de objetos for (int i = 0; i < listaConta.length; i++) {System.out.println(“Saldo da conta ” +listaConta[i].getNumero() + “: ” +listaConta[i].getSaldo());}

More Related