关于游戏中数组的应用

Array.prototype.minR = function() {//
 //
 /*this.sort(function (a, b) {
         return random(2)>0 ? 1 : -1;
        });*/
 x = this[0];
 k = 0;
 for (i=1; i<this.Length; i++) {
  y = this[i];
  if (y<x) {
   x = y;
   k = i;
  }
 }
 this[k] = this[this.length-1];
 this.length--;
 return x;
};
a = [1, 3, 4, 5];
trace(a.minR());
trace(a);
Array.prototype.del = function(i) {//刪除第i個元素
 x = this[i-1];
 for (j=i-1; j<this.length-1; j++) {
  this[j] = this[j+1];
  this.length--;
  return x;
 }
};
//a.del(0);
//trace(a);
Array.prototype.insert = function(i, x) {//從第i個位置插入x
 for (j=this.length-1; j>=i-1; j--) {
  this[j+1] = this[j];
 }
 this[i-1] = x;
 //this.length++;
};
a.insert(2, 10000000);
//trace(a);
Array.prototype.delX = function(x) {
 //從數組中刪除具有給定直的X的所有元素
 i = 0;
 while (i<this.length) {
  if (this[i] == x) {
   for (j=i+1; j<this.length; j++) {
    this[j-1] = this[j];
   }
   this.length--;
  } else {
   i++;
  }
 }
};
b = [0, 0, 1, 2, 3, 4,5,5];
b.delX(0);
trace(b);
Array.prototype.delChongfu = function() {//刪除所有重複的元素
 i = 0;
 while (i<this.length) {
  j = i+1;
  while (j<this.length) {
   if (this[j] == this[i]) {
    for (k=j+1; k<this.length; k++) {
     this[k-1] = this[k];
    }
    this.length--;
   } else {
    j++;
   }
  }
  i++;
 }
};
b.delChongfu()
trace(b);

n=getTimer();
Array.prototype.randomize = function() {
this.sort(function(a, b) { return random(2)>0 ? 1 : -1;});
}
var myArray = new Array();
for(var i=0;i<1000;i++){
myArray[i]=i; 
}
myArray.randomize();
trace(myArray); 
trace(getTimer()-n);
//
Array.prototype.duplicate = function(){
var a = [];
for (var i in this) (this[i] instanceof Array) ? a[i] = this[i].slice() : a[i] = this[i];
return a;
}
var myArray:Array = new Array();
myArray[0]=[1,2]
myArray[1]=3
myArray[2]=[5,6]
var temp:Array = myArray.duplicate();
trace(myArray); //1,2,3,5,6
trace(temp); //1,2,3,5,6
myArray[0][0] = 5;
trace(myArray); //5,2,3,5,6
trace(temp); //1,2,3,5,6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值