代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>javascript数组实例_www.jb200.com</title> <script type="text/javascript"> //王大爷有6只乌龟,它们的体重分别是3kg,5kg,1kg, //3.4kg,2kg,50kg 。请问这六只乌龟的总体重是少? //平均体重是多少? 请你用现在掌握的技术编一个程序解决 /* var weight=[3.4,2,50]; var total=0; //alert(weight); //alert(weight.length); for(var i=0;i<weight.length;i++){ total +=weight[i]; } //toFixed方法:把数字转换为字符串,结果的小数点后有指定位数的数字 alert("总体重为:"+total+" "+"平均体重为:"+(total/weight.length).toFixed(2)); */ /* //数组在函数中传递是地址 var weight=[20,45,80]; alert("函数调用前:"+weight); function test(arr){ //weight[0]=50; weight=null; } test(weight); alert("函数调用后:"+weight); */ /* var a =[2,3]; a[3]=6; //alert(a[2]); var str ="hello wo men zai"; var arr=str.split(" ",3); alert(typeof(arr)); */ //遍历一维数组 // var arr=[1.22,20,"hello",90,"world"]; /* //for 循环 for(var i=0;i<arr.length;i++){ document.writeln(arr[i]); }*/ /* //while var i=0 while(i<arr.length){ document.writeln(arr[i]); i++; }*/ //do...while /* var i=0; do{ document.writeln(arr[i]); i++; }while(i<arr.length); */ //for [other] /* for(var key in arr){ document.writeln(arr[key]); } */ //多维数组---二维数组 var arr=[["beijing",123,4.5],["a","b","c"]]; //var arr=[[0,0,0,0,0,0],[0,0,1,0,0,0],[0,2,0,3,0,0],[0,0,0,0,0,0]] //alert(arr[0][0]); //遍历二维数组 //alert(arr.length);//为2 //alert(arr[2]); //alert(arr[2][2]); for(var i=0;i<arr.length;i++){ //var arr2=arr[i]; for(var j=0;j<arr[i].length;j++){ document.writeln(arr[i][j]); } document.writeln("<br/>"); } </script> </head> <body> </body> </html>