Arrays
- Objects with some special functionality
- Array elements can be primitive values or references to other objects
- Length is dynamic - the
length
property stores the length
- Array objects can be created in two ways, with
new
, or by assigning an array literal
var myList = new Array(24, "bread", true);
//The elements of an array need not have the same type
var myList2 = [24, "bread", true];
var myList3 = new Array(24);
//a single parameter => the number of elements
- The length of an array is the highest subscript to which an element has been assigned, plus 1
myList[122] = "bitsy"; // length is 123
- Because the
length
property is writeable, you can set it to make the array any length you like, as in
myList.length = 150;
- Assigning a value to an element that does not exist creates that element
- Only the assigned elements of an array acually occupy space.
The length property of an array is not neccessarily the number of elements allocated.
Insert names to a sorted list
<!DOCTYPE html>
<!-- insert_names.html
A document for insert_names.js
-->
<html lang = "en">
<head>
<title> Name list </title>
<meta charset = "utf-8" />
</head>
<body>
<script type = "text/javascript" src = "insert_names.js" >
</script>
</body>
</html>
// insert_names.js
// The script in this document has an array of
// names, name_list, whose values are in
// alphabetic order. New names are input through
// prompt. Each new name is inserted into the
// name array, after which the new list is
// displayed.
// The original list of names
var name_list = new Array("Al", "Betty", "Kasper",
"Michael", "Roberto", "Zimbo");
var new_name, index, last;
// Loop to get a new name and insert it
while (new_name =
prompt("Please type a new name", "")) {
// Loop to find the place for the new name
last = name_list.length - 1;
while (last >= 0 && name_list[last] > new_name) {
name_list[last + 1] = name_list[last];
last--;
}
// Insert the new name into its spot in the array
name_list[last + 1] = new_name;
// Display the new array
document.write("<p><b>The new name list is:</b> ",
"<br />");
for (index = 0; index < name_list.length; index++)
document.write(name_list[index], "<br />");
/* There is another way to go over every element as below:
for(a in name_list)
document.write(name_list[a], "<br />");
*/
document.write("</p>");
} //** end of the outer while loop
- Array methods:
join
- converts all of the elements of an array to strings and catenates them into a single string.
e.g., var list = new Array("Discrete Structure", "Java II", "Database Programming");
var listStr = list.join(" : ");
The value of listStr is now "Discrete Structure : Java II : Database Programming"
Without providing any parameter, the strings are seperated by commas.
reverse
- reverse the order of the elements of the Array.
sort
- e.g., names.sort();
- Coerces elements to strings and puts them in alphabetical order
concat
- e.g., var list = new Array(3, 7, 9, 13);
var new newList = list.concat(47, 26);
The newList is [3,7,9,13,47,26].
slice
var list = [2, 4, 6, 8, 10];
listPart = list.slice(1, 3); //listPart is [4, 6]
listPart2 = list.slice(2); //listPart2 is [6, 8, 10]
var str ="This is a test.";
words = str.slice(" "); //words is an array with contents of each character:
toString
- Coerces elements to strings, if necessary, and
catenates them together, separated by commas
(exactly like join(", ")
)
push, pop, unshift,
and shift
The pop and push methods respectively remove and add an element to the high end of an array.
The shift and unshift methods respectively remove and add an element to the beginning of an array.
var list = new Array("Derrion", "Tom", "Roger");
document.write("The original list is ", list, "<br />");
var lastone = list.pop();
document.write("The lastone is ", lastone, "<br />");
document.write("After pop, the list is ", list, "<br />");
list.push("Chu");
document.write("After push, the list is ", list, "<br />");
var beginning = list.shift();
document.write("The beginning one is ", beginning, "<br />");
document.write("After shift, the list is ", list, "<br />");
list.unshift("Austin");
document.write("After unshift, the list is ", list, "<br />");
Two-dimensional array
<!DOCTYPE html>
<!-- nested_arrays.html
A document for nested_arrays.js
-->
<html lang = "en">
<head>
<title> Array of arrays </title>
<meta charset = "utf-8" />
</head>
<body>
<script type = "text/javascript" src = "nested_arrays.js" >
</script>
</body>
</html>
// nested_arrays.js
// An example illustrate an array of arrays
// Create an array object with three arrays as its elements
var nested_array = [[2, 4, 6],
[1, 3, 5],
[10, 20, 30]
];
// Display the elements of nested_list
for (var row = 0; row <= 2; row++) {
document.write("Row ", row, ": ");
for (var col = 0; col <=2; col++)
document.write(nested_array[row][col], " ");
document.write("<br />");
}