JSP(数组)
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>
上一篇: 用PS制作装有红色液体的质感玻璃球
下一篇: 西瓜能吃吗,西瓜的营养价值有哪些呢?