-------------------------------------------------------------------------------------------------
一维数组
-------------------------------------------------------------------------------------------------
<html>
<head>
<script
language="Javascript">
function ArrayDemo()
{
//
数组的创建
// Demo
1
var arr1 =
[‘e‘,‘l‘,‘l‘];
// Demo
2
var arr2 = new
Array(4);
arr2[0] =
‘W‘;
arr2[1] =
‘o‘;
arr2[2] =
‘r‘;
arr2[3] =
‘d‘;
// Demo
3
var arr3 = new
Array(‘1!‘,‘!‘,‘3!‘);
document.write(‘arr1:‘ + arr1.toString() +
‘<br>‘);
document.write(‘arr2:‘ + arr2.toString() +
‘<br>‘);
document.write(‘arr3:‘ + arr3.toString() +
‘<br>‘);
// Demo
4
var arr4 =
[1,11,21,31,41,51,52,54,70,100];
var arr5 = [2,3,4,5];
document.write(‘arr4:‘ + arr4.toString() +
‘<br>‘);
document.write(‘arr5:‘ + arr5.toString() +
‘<br>‘);
//
push,pop方法
arr1.push(‘o‘); //
从数组的最后添加‘o‘
arr1.unshift(‘H‘); //
从数组的前面添加‘H‘
document.write(‘从arr1的后面push(o),从前面unshift(H)后的结果:‘ + arr1.toString() +
‘<br>‘); // Hello
document.write(‘从arr3的最后pop:‘ + arr3.pop() + ‘<br>‘); //
从数组最后去掉一个元素
document.write(‘从arr3的前面shift:‘ + arr3.shift() + ‘<br>‘);//
从数组的开始去掉一个元素
document.write(‘arr3结果:‘ + arr3.toString() +
‘<br>‘);
arr2.splice(3,2,‘l‘,‘d‘); //
arrayObject.splice(index,howmany,item1,.....,itemX)
document.write(‘从arr2的第(3)位开始插入2个元素后结果:‘ + arr2.toString() +
‘<br>‘);
arr2.splice(4,1);
document.write(‘从arr2的第(4)位开始删除一个元素后结果:‘ + arr2.toString() +
‘<br>‘);
document.write(‘取arr2的第1位到第3位,但不包括第3位:‘ + arr2.slice(1,3) +
‘<br>‘);
document.write(‘arr4与arr5合并后,并不改变原有数组,而是生成一个新的数组:‘ +
arr4.concat(arr5).toString() +
‘<br>‘);
document.write(‘合并后的arr4:‘ + arr4.toString() +
‘<br>‘);
document.write(‘合并后的arr5:‘ + arr5.toString() +
‘<br>‘);
return;
}
</script>
</head>
<body>
<input value="click" type="button"
onclick="ArrayDemo();">
</body>
</html>
-------------------------------------------------------------------------------------------------
二维数组
-------------------------------------------------------------------------------------------------
<html>
<head>
<script
language="Javascript">
function
DoubleArrayDemo() {
// Demo
1
var arr1 = new
Array([‘e1‘,‘l1‘,‘l1‘],[‘e2‘,‘l2‘,‘l2‘]);
document.write(arr1.length +
"</br>");
for
(i=0;i<arr1.length;i++){
for
(j=0;j<arr1[i].length;j++){
document.write(arr1[i][j] +
"</br>");
}
}
return;
}
</script>
</head>
<body>
<input value="click" type="button"
onclick="DoubleArrayDemo();">
</body>
</html>