数组

一、数组的声明:

1、一维数组

var x=new Array();//空数组

var x = new Array(size); //指定长度的数组

var x = new Array(element 0,element 1,……);//指定初始值,,,,不限定存放的数据类型

还可以: var x =[“小明”, “小红” ] ;//表示一维数组数组元素 ----小明,小红

但是var x=[3] 则表示数组x的长度为3;

2、

二维数组 用数组嵌套实现二维或多维数组

Eg:arr[0] = {‘你’ , ‘好’ , ‘啊’};

arr[1] = {‘你’, ‘真’,‘美’ };

arr[2] = {‘you’ , ‘are’ , ‘full’};

//遍历二维数组

for(var i=0; i>



数组
<script type="text/javascript">
var a = new Array();
do {
var name = prompt('请输入字符串, 想要结束请输入“END"结束', 'END');//'END' 为默认填写的内容
a[a.length] = name;
} while (name != 'END');
a.length = a.length - 1;
alert(a);


var b = new Array();
b[0] = ["ni", "hao"];
b[1] = [3, 4];
//for (var i = 0; i < b.length; i++)
// for (var j = 0; j < b[i].length; j++)
// {
// document.write(b[i][j]+"
")
// }
for (var i in b)
for (var j in b[i])
{
document.write(b[i][j] + "
")
}
var b = new Array('小崽', '重载', '打在');


for (var i = 0; i < b.length; i++) {
alert(b[i]);
}
//concat()连接
document.write(b.concat());


//join()把数组的所有元素放入一个字符
document.write(b.join("!"));
document.write("
");


//pop()删除最后一个元素并输出 删除后的数组
document.write(b.pop());


//tostring()方法;
var arr = new Array();
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "
");


//for(var i in arr)
//for (var i in arr) {
// document.write(i + arr[i]);


/