Actionscript 3 在ActionScript(3.0)中干净地合并两个数组?

Actionscript 3 在ActionScript(3.0)中干净地合并两个数组?,actionscript-3,arrays,actionscript,merge,Actionscript 3,Arrays,Actionscript,Merge,在ActionScript(特别是ActionScript 3.0)中合并两个排序数组的好方法是什么?生成的数组应进行排序且不重复。要合并(连接)数组,请使用.concat() 下面是两个示例,说明如何连接数组并同时删除重复项 更方便的方法:(您可以使用ArrayUtil.createUniqueCopy()from) 稍微快一点的方法:(您可以自己循环数组并使用它检查重复项) 这是一种简单的算法。如果在Actionscript中有更直接的方法来实现这一点,我会感到惊讶 function mer

在ActionScript(特别是ActionScript 3.0)中合并两个排序数组的好方法是什么?生成的数组应进行排序且不重复。

要合并(连接)数组,请使用
.concat()

下面是两个示例,说明如何连接数组并同时删除重复项

更方便的方法:(您可以使用
ArrayUtil.createUniqueCopy()
from)

稍微快一点的方法:(您可以自己循环数组并使用它检查重复项)


这是一种简单的算法。如果在Actionscript中有更直接的方法来实现这一点,我会感到惊讶

function merge(a1:Array, a2:Array):Array {
    var result:Array = [];
    var i1:int = 0, i2:int = 0;

    while (i1 < a1.length && i2 < a2.length) {
        if (a1[i1] < a2[i2]) {
            result.push(a1[i1]);
            i1++;
        } else if (a2[i2] < a1[i1]) {
            result.push(a2[i2]);
            i2++;
        } else {
            result.push(a1[i1]);
            i1++;
            i2++;
        }
    }

    while (i1 < a1.length) result.push(a1[i1++]);
    while (i2 < a2.length) result.push(a2[i2++]);

    return result;
}
函数合并(a1:数组,a2:数组):数组{
变量结果:数组=[];
变量i1:int=0,i2:int=0;
而(i1
如果您有一个包含大量元素的列表,那么使用Array.indexOf检测重复项的速度会非常慢;删除重复数据的一种更快的方法是,在将数组的内容连接起来后,将其放入一个数组中

// Combine the two Arrays.
const combined : Array = a.concat(b);

// Convert them to a Set; this will knock out all duplicates.
const set : Object = {};  // use a Dictionary if combined contains complex types.

const len : uint = combined.length;
for (var i : uint = 0; i < len; i++) {
    set[combined[i]] = true;
}

// Extract all values from the Set to produce the final result.
const result : Array = [];
for (var prop : * in set) {
    result.push[prop];
}
//组合这两个数组。
常数组合:数组=a.concat(b);
//把它们转换成一个集合;这将删除所有重复项。
常量集:对象={};//如果组合包含复杂类型,请使用字典。
const len:uint=组合长度;
对于(变量i:uint=0;i
如果您的程序大量使用集合,那么If可能会谨慎地使用许多AS3集合框架中的一个,这些框架为操作数据提供了一个简单的接口,并且在实现时将始终采用最佳方法


请按照以下步骤获得答案:

  • Concat两个数组使用“Concat”方法
  • 使用数组类中作为API提供的“sort”方法进行新的数组(concated)排序
  • 生成用户定义的函数以删除重复项(请参见下面的函数)
  • >函数移除副本(p_arr:Array):Array{
    var-ansArr:Array=new-Array();
    var len:uint=p_arr.length;
    变量i:uint=0;
    变量j:uint=0;
    ansArr[j]=p_arr[i];
    i++;
    j++;
    
    虽然(我要求一个“好”的方式…:)好的,也许我在答案中添加的另一种方式可以被认为是“好”的非常简洁,谢谢。
    result.push[prop]
    tho.应该是
    result.push(prop)
    function merge(a1:Array, a2:Array):Array {
        var result:Array = [];
        var i1:int = 0, i2:int = 0;
    
        while (i1 < a1.length && i2 < a2.length) {
            if (a1[i1] < a2[i2]) {
                result.push(a1[i1]);
                i1++;
            } else if (a2[i2] < a1[i1]) {
                result.push(a2[i2]);
                i2++;
            } else {
                result.push(a1[i1]);
                i1++;
                i2++;
            }
        }
    
        while (i1 < a1.length) result.push(a1[i1++]);
        while (i2 < a2.length) result.push(a2[i2++]);
    
        return result;
    }
    
    function remDuplicates(_array:Array):void{
        for (var i:int = 0; i < _array.length;++i) {
            var index:int = _array.indexOf(_array[i]);
            if (index != -1 && index != i) {
                _array.splice(i--, 1);
            }
        }
    }
    
    var testArray:Array = [1, 1, 1, 5, 4, 5, 5, 4, 7, 2, 3, 3, 6, 5, 8, 5, 4, 2, 4, 5, 1, 2, 3, 65, 5, 5, 5, 5, 8, 4, 7];
    var testArray2:Array = [1, 1, 1, 5, 4, 5, 5, 4, 7, 2, 3, 3, 6, 5, 8, 5, 4, 2, 4, 5, 1, 2, 3, 65, 5, 5, 5, 5, 8, 4, 7];
    
    testArray.concat(testArray2);
    trace(testArray);
    remDuplicates(testArray);
    trace(testArray);
    
    // Combine the two Arrays.
    const combined : Array = a.concat(b);
    
    // Convert them to a Set; this will knock out all duplicates.
    const set : Object = {};  // use a Dictionary if combined contains complex types.
    
    const len : uint = combined.length;
    for (var i : uint = 0; i < len; i++) {
        set[combined[i]] = true;
    }
    
    // Extract all values from the Set to produce the final result.
    const result : Array = [];
    for (var prop : * in set) {
        result.push[prop];
    }
    
     var ansArr:Array = new Array();
     var len:uint = p_arr.length;
     var i:uint = 0;
     var j:uint = 0;
     ansArr[j] = p_arr[i];
     i++;
     j++;
     while(i<len)
     {
        if(ansArr[j] != p_arr[i])
        { 
          ansArr[j] = p_arr[i];
          j++;
        }
        i++;
     }
     return ansArr;