1,数组列表
var arr=["zx","123","4.7"];
每个数组都有length属性
var arr=[1, , , 5];
为空的元素为undefeated
var arr=["x+"+1, 12.3];
数组元素可以是表达式
var arr=[[1,2], "hello","3.8"];
数组元素本身也可以是数组
<html> <script language="javascript"> var arr = [1, ["zxx"+1, ,"li",3.5],["2","@","1"]]; for(var i = 0; i<arr.length;i++) { if(arr[i].length) { for(var j=0; j<arr[i].length;j++) { alert(arr[i][j]); } } else { alert(arr[i]); } } </script> </html>
2,用对象的方式实现数组
<html> <script language="javascript"> function MyArray() { this.length=arguments.length; for(var i;i < this.length;i++) { this[i]=arguments[i]; } } var str=""; var arr=new MyArray(4,3.5,"abo"); for(var i = 0;i < arr.length;i++) { str +=arr[i] + "\n"; } alert(str); function MyArray2(size) { this.length=size; for(var i=0;i<size;i++) { this[i]=""; } } var arr2=new MyArray2(2); arr[0]=3; arr[1]="ba"; arr[2]=4; var x,str=""; for(x in arr) { str += x + ":" + arr[x]+"\n"; } alert(str); </script> </html>
<html> <script language="javascript"> function MyArray() { this.length=arguments.length; for(var i;i < this.length;i++) { this[i]=arguments[i]; } } var str=""; var arr=new MyArray(4,3.5,"abo"); for(var i = 0;i < arr.length;i++) { str +=arr[i] + "\n"; } alert(str); function MyArray2(size) { this.length=size; for(var i=0;i<size;i++) { this[i]=""; } } var arr2=new MyArray2(2); arr[0]=3; arr[1]="ba"; arr[2]=4; var x,str=""; for(x in arr) { str += x + ":" + arr[x]+"\n"; } alert(str); </script> </html>
3,Array对象
* Array()
<html> <script language="javascript"> var arr = new Array(); arr[0]='1'; arr[1]=2; alert(arr.length); </script> </html>
* Array(4):确定长度
* Array(3.5,"abc",3)
<html> <script language="javascript"> var arr = new Array(); arr[0]='1'; arr[1]=2; arr[3]=3; arr[4]=0; arr.sort(); var str=""; for(var x in arr) { str+=x+":"+arr[x]+"\n"; } alert(str); alert(arr.length); </script> </html>