5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:
性别:秘密
最后登录:2007-09-22
http://yuheduo.5d.cn/
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2005/08/15 | 数组的一些应用
类别(Flash学习)
|
评论
(0)
|
阅读(110)
|
发表于 21:51
很实用的功能,经常可能用到,代码来自帝国
功能:删除数组中的重复元素。
作者:***********
代码:
//函数
Array.prototype.unique=function(){
for(var y=0;y<this.length;++y){
for(var z=(y+1);z<=this.length;++z){
if(this[y]==this[z]){
this.splice(z,1);
z--;
}
}
}
}
//用法
a = [10,10,20,5,20,10];
trace(a);
a.unique();
trace(a);
功能:比较两个数组是否相等。
作者:senocular
代码:
//函数
Array.prototype.equals = function(a) {
if (this == a) {
return true;
}
var i = this.length;
if (i != a.length) {
return false;
}
while (i--) {
if (this[i] != a[i]) {
return false;
}
}
return true;
};
//用法
a = [1, 2, 3, 4];
b = [1, 2, 3, 4];
c = [1, 2, 3, 5];
d = a;
// true
trace(a.equals(b));
// false
trace(a.equals(c));
// true
trace(a.equals(d));
功能:随机排列数组内部元素的次序(分纸牌)。
作者:MSA
代码:
//函数
Array.prototype.exchange = function(i, j) {
if (i != j) {
with (this[i]) {
this[i] = this[j];
this[j] = valueOf();
}
}
};
Array.prototype.shuffle = function() {
var i = this.length-1;
if (i) {
do {
this.exchange(i, Math.floor(Math.random()*(i+1)));
} while (--i);
}
};
//用法
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
a.shuffle();
trace(a);
//输出结果:7,3,0,2,5,6,1,8,9,4
以上代码全部在 flashMX2004 AS2.0下测试通过.
原本还有一个二维数组的建立,但用的是MX以前的方法,无法在flashMX2004 AS2.0下测试通过,我简单写了一个 以前我也写过数组排序,数组比较等函数...
代码:
//很简单也没什么好注释的了
myArray = new Array();
for (i=0; i<3; i++) {
myArray[i] = new Array();
for (j=0; j<3; j++) {
myArray[i][j] = "Array"+i+j;
}
}
trace(myArray);
------------------------------------
Flash数组成员按降幂排序的方法代码:
aa = new Array(4, 3, 5, 1, 2);
for (var j = 5; j>0; j--) {
for (var i = 0; i<j; i++) {
if (aa[i]>aa[i+1]){
tt = aa[i+1];
aa[i+1] = aa[i];
aa[i] = tt;
}
}
}
trace(aa)
做排列做游戏的朋友也许经常会遇上这个,
0
评论
Comments
日志分类
首页
[149]
blog记事
[7]
Flash学习
[85]
教学记忆
[21]
娱乐影音
[9]
我的收藏
[27]
PhotoShop学习
[0]