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]
<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]
<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]
<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]
<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]
<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>