☆Dot Net☆ 【east_liujie】

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  11 随笔 :: 1 文章 :: 20 评论 :: 0 引用

new Array()
new Array(len)
new Array([item0,[item1,[item2,...]]]

使用数组对象的方法:

var objArray=new Array();

objArray.concact([item1[,item2[,....]]]-------------------将参数列表连接到objArray的后面形成一个新的数组并返回,原有数组不受影响。如:var arr=["a","b","c"];
arr.concact("d","e");
将返回包括从"a"到"e"字母元素的数组。而arr本身不受影响。

objArray.join(separator)-----------------以separator指定的字符作为分割符,将数组转换为字符串,当seperator 为逗号时,其作用和toString()相同。

objArray.pop()---------通俗的讲,就是弹出数组的最后一个元素。结合下面的push方法,使得将数组作为栈来使用成为可能。pop方法返回数组最后一个元素的值,并将length属性减1,即返回后立即丢失最后一个元素。

objArray.push([value1[,value2[,....]]])-------------将参数添加到数组的结尾。如:[1,2,3, 4].push("a","b")将得到[1,2,3,4,"a","b"]

objArray.reverse()将数组中的元素反转排列。如:[1,2,3].reverse()将得到[3,2,1],这个操作是在原有数组上经行操作,同时也返回数组本身
objArray.shift()-----------移去数组的第一个元素,并返回这个元素的值。这个方法的性质和pop方法很类似,pop方法是移去最后一个元素。

objArray.slice(start,end)-----------返回数组对象的一个子集,索引从start开始(包括 start),到end结束(不包括end),原有数组不受影响。如:[1,2,3,4,5,6].slice(1,4)将得到[2,3,4]。当start或者end为负数时,则使用他们加上length后地值。如:[1,2,3,4,5,6].slice(-4,-1)将得到[3,4,5]。如果end小于等于start,将返回空数组。

objArray.sort(comparefn)-------根据comparefn定义的大小比较函数,对一个数组进行排序。函数comparefn必须接受两个参数element1,element2,如果需要需要element1排在element2之前,应该返回一个负数;如果需要element1排在element2之后,应该返回一个正数,如果两个数平等对待(即保持原有顺序)则返回0。当省略comparefn时,则元素按照字典顺序排列。如:对定义的比较函数cmp: function cmp(e1,e2){return e1-e2;}则[3,4,2,7].sort(cmp)将得到[2,3,4,7].
objArray.splice(start,deleteCount[,item1,item2[,...]]])这是一个复杂的函数,用于完成数组元素的删除 取代和插入操作。其中,start参数表示要进行操作的索引位置,deleteCount指从start开始要删除的元素的元素个数(包括了start位置),如果deleteCount省略,则表示从start开始要删除数组的剩余部分。[,item1[,item2[,...]]]则表示可选的插入到start之前的元素列表。如:

var arr=[0,1,2,3,4,5,6];

arr.splice(1,1);

document.write(arr);//显示“0,2,3,4,5,6”

arr=[0,1,2,3,4,5,6];

arr.splice(0,0,"a","b");

document.write(arr);//显示“a,b,0,1,2,3,4,5,6"

arr=[0,1,2,3,4,5,6];

arr.splice(3,2,"c","d");

document.write(arr);//显示"0,1,2,c,d,5,6"

objArray.unshift(item1[,item2[,...]]])-------------------将参数列表插入到数组的开头。其性质和push方法类型,但push方法是将元素添加到数组的结尾。如:[1,2,3,4].unshift("a","b")将得到["a","b",1,2,3,4]。



从大到小冒泡排序法:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<script language="JavaScript" type="text/javascript">
<!--
function bubbleSort(arr){
    
//外层循环,共要进行arr.length次求最大值操作
    for(var i=0;i<arr.length;i++){
        
//内层循环,找到第i大的元素,并将其和第i个元素交换
        for(var j=i;j<arr.length;j++){
            
if(arr[i]<arr[j]){
                
//交换两个元素的位置
                var temp=arr[i];
                arr[i]
=arr[j];
                arr[j]
=temp;
            }
        }
    }
}

var arr=[32,55,78,43,78,10,45,20,9,89];
bubbleSort(arr);

//输出:89,78,78,55,45,43,32,20,10,9
for(var i=0;i<arr.length;i++){
    document.write(arr[i]
+",");
}
//-->
</script>

</body>
</html>

posted on 2006-10-21 17:36 东方一杰 阅读(23576) 评论(9) 编辑 收藏

评论

#1楼 2008-01-14 13:12 木子文武      
楼主写的很详细,不错!
 回复 引用 查看   

#2楼 2008-03-03 15:26 吴[未注册用户]
很好,很和谐。
 回复 引用   

#3楼 2008-03-12 14:32 CODI[未注册用户]
你好,

我想宣告一个二维阵列,但是一直出错。
我想要
shichen["甲"][1]="甲子";
shichen["甲"][2]="乙丑";
......
shichen["乙"][1]="丙子";
shichen["乙"][1]="丁丑";。。。。。

我该怎么宣告?下面看起来都执行不了。。。
var shichen = ["甲","己","乙","庚","丙","辛","丁","壬","戊","癸"];
shichen["甲"][] = ["甲子","乙丑","丙寅","丁卯","戊辰","己巳","庚午","辛未","壬申","癸酉","甲戌","乙亥"];
shichen["己"][] = ["甲子","乙丑","丙寅","丁卯","戊辰","己巳","庚午","辛未","壬申","癸酉","甲戌","乙亥"];
shichen["乙"][] = ["丙子","丁丑","戊寅","己卯","庚辰","辛巳","壬午","癸未","甲申","乙酉","丙戌","丁亥"];
shichen["庚"][] = ["丙子","丁丑","戊寅","己卯","庚辰","辛巳","壬午","癸未","甲申","乙酉","丙戌","丁亥"];
shichen["丙"][] = ["戊子","己丑","庚寅","辛卯","壬辰","癸巳","甲午","乙未","丙申","丁酉","戊戌","己亥"];
shichen["辛"][] = ["戊子","己丑","庚寅","辛卯","壬辰","癸巳","甲午","乙未","丙申","丁酉","戊戌","己亥"];
shichen["丁"][] = ["庚子","辛丑","壬寅","癸卯","甲辰","乙巳","丙午","丁未","戊申","己酉","庚戌","辛亥"];
shichen["壬"][] = ["庚子","辛丑","壬寅","癸卯","甲辰","乙巳","丙午","丁未","戊申","己酉","庚戌","辛亥"];
shichen["戊"][] = ["壬子","癸丑","甲寅","乙卯","丙辰","丁巳","戊午","己未","庚申","辛酉","壬戌","癸亥"];
shichen["癸"][] = ["壬子","癸丑","甲寅","乙卯","丙辰","丁巳","戊午","己未","庚申","辛酉","壬戌","癸亥"];

 回复 引用   

--引用--------------------------------------------------
CODI: 你好,

我想宣告一个二维阵列,但是一直出错。
我想要
shichen[&quot;甲&quot;][1]=&quot;甲子&quot;;
shichen[&quot;甲&quot;][2]=&quot;乙丑&quot;;
......
shichen[&quot;乙&quot;][1]=&quot;丙子&quot;;
shichen[&quot;乙&quot;][1]=&quot;丁丑&quot;;。。。。。

我该怎么宣告?下面看起来都执行不了。。。
var shichen = [&quot;甲&quot;,&quot;己&quot;,&quot;乙&quot;,&quot;庚&quot;,&quot;丙&quot;,&quot;辛&quot;,&quot;丁&quot;,&quot;壬&quot;,&quot;戊&quot;,&quot;癸&quot;];
shichen[&quot;甲&quot;][] = [&quot;甲子&quot;,&quot;乙丑&quot;,&quot;丙寅&quot;,&quot;丁卯&quot;,&quot;戊辰&quot;,&quot;己巳&quot;,&quot;庚午&quot;,&quot;辛未&quot;,&quot;壬申&quot;,&quot;癸酉&quot;,&quot;甲戌&quot;,&quot;乙亥&quot;];
shichen[&quot;己&quot;][] = [&quot;甲子&quot;,&quot;乙丑&quot;,&quot;丙寅&quot;,&quot;丁卯&quot;,&quot;戊辰&quot;,&quot;己巳&quot;,&quot;庚午&quot;,&quot;辛未&quot;,&quot;壬申&quot;,&quot;癸酉&quot;,&quot;甲戌&quot;,&quot;乙亥&quot;];
shichen[&quot;乙&quot;][] = [&quot;丙子&quot;,&quot;丁丑&quot;,&quot;戊寅&quot;,&quot;己卯&quot;,&quot;庚辰&quot;,&quot;辛巳&quot;,&quot;壬午&quot;,&quot;癸未&quot;,&quot;甲申&quot;,&quot;乙酉&quot;,&quot;丙戌&quot;,&quot;丁亥&quot;];
shichen[&quot;庚&quot;][] = [&quot;丙子&quot;,&quot;丁丑&quot;,&quot;戊寅&quot;,&quot;己卯&quot;,&quot;庚辰&quot;,&quot;辛巳&quot;,&quot;壬午&quot;,&quot;癸未&quot;,&quot;甲申&quot;,&quot;乙酉&quot;,&quot;丙戌&quot;,&quot;丁亥&quot;];
shichen[&quot;丙&quot;][] = [&quot;戊子&quot;,&quot;己丑&quot;,&quot;庚寅&quot;,&quot;辛卯&quot;,&quot;壬辰&quot;,&quot;癸巳&quot;,&quot;甲午&quot;,&quot;乙未&quot;,&quot;丙申&quot;,&quot;丁酉&quot;,&quot;戊戌&quot;,&quot;己亥&quot;];
shichen[&quot;辛&quot;][] = [&quot;戊子&quot;,&quot;己丑&quot;,&quot;庚寅&quot;,&quot;辛卯&quot;,&quot;壬辰&quot;,&quot;癸巳&quot;,&quot;甲午&quot;,&quot;乙未&quot;,&quot;丙申&quot;,&quot;丁酉&quot;,&quot;戊戌&quot;,&quot;己亥&quot;];
shichen[&quot;丁&quot;][] = [&quot;庚子&quot;,&quot;辛丑&quot;,&quot;壬寅&quot;,&quot;癸卯&quot;,&quot;甲辰&quot;,&quot;乙巳&quot;,&quot;丙午&quot;,&quot;丁未&quot;,&quot;戊申&quot;,&quot;己酉&quot;,&quot;庚戌&quot;,&quot;辛亥&quot;];
shichen[&quot;壬&quot;][] = [&quot;庚子&quot;,&quot;辛丑&quot;,&quot;壬寅&quot;,&quot;癸卯&quot;,&quot;甲辰&quot;,&quot;乙巳&quot;,&quot;丙午&quot;,&quot;丁未&quot;,&quot;戊申&quot;,&quot;己酉&quot;,&quot;庚戌&quot;,&quot;辛亥&quot;];
shichen[&quot;戊&quot;][] = [&quot;壬子&quot;,&quot;癸丑&quot;,&quot;甲寅&quot;,&quot;乙卯&quot;,&quot;丙辰&quot;,&quot;丁巳&quot;,&quot;戊午&quot;,&quot;己未&quot;,&quot;庚申&quot;,&quot;辛酉&quot;,&quot;壬戌&quot;,&quot;癸亥&quot;];
shichen[&quot;癸&quot;][] = [&quot;壬子&quot;,&quot;癸丑&quot;,&quot;甲寅&quot;,&quot;乙卯&quot;,&quot;丙辰&quot;,&quot;丁巳&quot;,&quot;戊午&quot;,&quot;己未&quot;,&quot;庚申&quot;,&quot;辛酉&quot;,&quot;壬戌&quot;,&quot;癸亥&quot;];


--------------------------------------------------------
因为要对以为数组再次声明才能使用二维....
 回复 引用   

#5楼 2008-03-14 19:03 CODI[未注册用户]
那么具体该怎么再次声明呢? 谢谢!
 回复 引用   

#6楼 2008-03-14 19:06 CODI[未注册用户]
我用了
var shichen = new array()l
shichen["甲"] = new array();
shichen["甲"][1] = "something ....
这样的描述,也被接受不了。谢谢!
 回复 引用   

#7楼 2009-05-13 10:52 sdf[未注册用户]
--引用--------------------------------------------------
木子文武: 楼主写的很详细,不错!
--------------------------------------------------------
sfdf
 回复 引用   

#8楼 2009-09-10 11:18 飞鸽传书      
Array 可用,不过就难理解点。
 回复 引用 查看   

#9楼 2010-05-07 15:50 walkerwang      
学习了
 回复 引用 查看