T13_LM3: Arrays (2013-2014)

22
JavaScript: métodos Array (cont.) + arrays^n Carlos Santos LabMM 3 - NTC - DeCA - UA Aula 13, 30-10-2013

description

 

Transcript of T13_LM3: Arrays (2013-2014)

Page 1: T13_LM3: Arrays (2013-2014)

JavaScript: métodos Array (cont.) + arrays^n

Carlos SantosLabMM 3 - NTC - DeCA - UAAula 13, 30-10-2013

Page 2: T13_LM3: Arrays (2013-2014)

JavaScript: objeto Array: métodos

• Os mais comuns... mas existem mais!Método Descriçãoconcat() Joins two or more arrays, and returns a copy of the joined arrays

join() Joins all elements of an array into a string

pop() Removes the last element of an array, and returns that element

push() Adds new elements to the end of an array, and returns the new length

reverse() Reverses the order of the elements in an array

shift() Removes the first element of an array, and returns that element

slice() Selects a part of an array, and returns the new array

sort() Sorts the elements of an array

splice Adds/Removes elements from an array

toString() Converts an array to a string, and returns the result

unshift() Adds new elements to the beginning of an array, and returns the new lengthhttp://www.w3schools.com/jsref/jsref_obj_array.asp

Page 3: T13_LM3: Arrays (2013-2014)

Array: métodos: slice()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.slice(0,1) + "<br />");!

document.write(fruits.slice(1) + "<br />");!

document.write(fruits.slice(-2) + "<br />");!

document.write(fruits);!

!// ??!

// ??!

// ??!

// ??

Page 4: T13_LM3: Arrays (2013-2014)

Array: métodos: slice()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.slice(0,1) + "<br />");!

document.write(fruits.slice(1) + "<br />");!

document.write(fruits.slice(-2) + "<br />");!

document.write(fruits);!

!// Banana!

// Orange,Apple,Mango!

// Apple,Mango!

// Banana,Orange,Apple,Mango

Page 5: T13_LM3: Arrays (2013-2014)

Array: métodos: sort()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.sort());!

!// ??

Page 6: T13_LM3: Arrays (2013-2014)

Array: métodos: sort()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.sort());!

!// Apple,Banana,Mango,Orange

Page 7: T13_LM3: Arrays (2013-2014)

Array: métodos: splice()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write("Added: " + fruits.splice(2,0,"Lemon") + "<br />");!

document.write(fruits);!

!// Added:!

// Banana,Orange,Lemon,Apple,Mango!

Page 8: T13_LM3: Arrays (2013-2014)

Array: métodos: splice()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write("Removed: " + fruits.splice(2,1,"Lemon") + "<br />");!

document.write(fruits);!

!// Removed: Apple!

// Banana,Orange,Lemon,Mango!

Page 9: T13_LM3: Arrays (2013-2014)

Array: métodos: splice()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write("Removed: " + fruits.splice(2,2,"Lemon") + "<br />");!

document.write(fruits);!

!// Removed: Apple,Mango!

// Banana,Orange,Lemon!

Page 10: T13_LM3: Arrays (2013-2014)

Array: métodos: toString()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.toString());!

!// Banana,Orange,Apple,Mango!

Page 11: T13_LM3: Arrays (2013-2014)

Array: métodos: unshift()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.unshift("Kiwi") + "<br />");!

document.write(fruits.unshift("Lemon","Pineapple") + "<br />");!

document.write(fruits);!

!// ??!

// ??!

// ??!

Page 12: T13_LM3: Arrays (2013-2014)

Array: métodos: unshift()

var fruits = ["Banana", "Orange", "Apple", "Mango"];!

document.write(fruits.unshift("Kiwi") + "<br />");!

document.write(fruits.unshift("Lemon","Pineapple") + "<br />");!

document.write(fruits);!

!// 5!

// 7!

// Lemon,Pineapple,Kiwi,Banana,Orange,Apple,Mango!

Page 13: T13_LM3: Arrays (2013-2014)

Estruturas de dados ainda mais complexas :)

• E se um elemento de um array não for um elemento de tipo simples?

• Exemplo: O Nelo não pode guardar só o nome das suas gajas. Ele precisa guardar o número de telemóvel e o email!

var gajasNelo = new Array();!

gajasNelo[0] = “Asdrubal, 961111111, [email protected]”;!

gajasNelo[1] = “Porfirio, 931111111, [email protected]”;!

gajasNelo[2] = “Zacarias, 911111111, [email protected]”;

Page 14: T13_LM3: Arrays (2013-2014)

Problemas com solução anterior?

• Os elementos não são unidades de informação

• Logo, o acesso a unidades de informação não pode ser realizado de uma forma simples e sistemática

• “Voltamos ao problema de misturar alhos com bugalhos!”

!

• Então como resolver?

• Pista para a nova solução: “Cada elemento de um array pode ter associado um tipo de dados complexo”

Page 15: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais

Asdrubal 961111111 [email protected]

Porfirio 931111111 [email protected]

Zacarias 911111111 [email protected]

0

1

2

nome telefone email

gajasNelo

Page 16: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais

“Asdrubal” “961111111” “[email protected]

“Porfirio” “931111111” “[email protected]

“Zacarias” “911111111” “[email protected]

0

1

2

0 1 2

gajasNelo

Page 17: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais

0

1

2

“Asdrubal" “961111111” “[email protected]

0 1 2

“Porfirio” “931111111” “[email protected]”0 1 2

“Zacarias” “911111111” “[email protected]

0 1 2

gajasNelo

Page 18: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais: declaração

var gajasNelo = new Array();!

gajasNelo[0] = new Array("Asdrubal","961111111","[email protected]");!

gajasNelo[1] = new Array("Porfirio","931111111","[email protected]");!

gajasNelo[2] = new Array("Zacarias","911111111","[email protected]");!

!// Outra possibilidade!

var gajasNelo = new Array();!

gajasNelo[0] = ["Asdrubal","961111111","[email protected]"];!

gajasNelo[1] = ["Porfirio","931111111","[email protected]"];!

gajasNelo[2] = ["Zacarias","911111111","[email protected]"];

Page 19: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais: declaração

// E ainda outra possibilidade!

var gajasNelo = new Array();!

gajasNelo[0] = new Array();!

gajasNelo[0][0] = "Asdrubal";!

gajasNelo[0][1] = "961111111";!

gajasNelo[0][2] = "[email protected]";!

gajasNelo[1] = new Array();!

gajasNelo[1][0] = "Porfirio";!

...

Page 20: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais

gajasNelo

“Asdrubal" “961111111” “[email protected]

“Porfirio” “931111111” “[email protected]

“Zacarias” “911111111” “[email protected]

0

1

2

0 1 2

alert(gajasNelo[1]); // ??!

alert(gajasNelo[0][2]); // ??!

alert(gajasNelo[2][1]); // ??!

gajasNelo[1][0] = “Marcão”; // ??!

gajasNelo[2][2] = gajasNelo[1][2]; // ??!

alert(gajasNelo.length); // ??!

alert(gajasNelo[1].length); // ??

Page 21: T13_LM3: Arrays (2013-2014)

Arrays multidimensionais

• Os princípios aplicados na criação de um array bidimensional são válidos para um array de qualquer dimensão;

• num array bidimensional, cada elemento pode ser também um array, resultando num array tridimensional;

• e esta lógica pode continuar a ser aplicada...

• Raramente se recorre a arrays com mais do que 3 dimensões porque começam a ser demasiado complexos de visualizar e são de difícil manutenção.

Page 22: T13_LM3: Arrays (2013-2014)

Arrays bidimensionais: exemplo

var tabuada = new Array();!

for (var linha=1; linha<=10;linha++){!

tabuada[linha] = new Array();!

for (var coluna = 1; coluna<=10; coluna++){!

tabuada[linha][coluna]=linha*coluna;!

}!

}!

!alert(tabuada[5][5]); // ??!

alert(tabuada[0][5]); // ??