数组找最大和他所在的位置

      被人教程 2005-7-11 11:22
很多小游戏要用到
写段代码

maxvalue = function (array) {
//找最大
mx = array[0];
for (i=0; i<array.length; i++) {
if (array[i]>mx) {
mx = array[i];
}
}
return mx;
};

k = new Array();
k = [12, 5, 63, 7, 1, -1, 352, 36, 754, 75, 56, 74];
trace(maxvalue(k)); // 754

//找位置

Array.prototype.getIndex = function(data) {
    for (i=0; i<this.length; ++i) {
        if (this[i] == data) {
            return i;
        }
    }
    return -1;
};
resources = new Array("a", "b", "c", "d", "e", "f", "g", "h");
location = resources.getIndex("e");
trace(location);

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}