gpt4 book ai didi

actionscript-3 - ActionScript - 比较和删除复杂数组的重复项?

转载 作者:行者123 更新时间:2023-12-01 12:59:50 25 4
gpt4 key购买 nike

当比较简单数组时,我使用类似于以下函数的东西来连接和删除重复项:

//Merge
public function merge(a1:Array, a2:Array):Array
{
var result:Array = a1.concat(a2);

var dictionary:Dictionary = new Dictionary();

for each (var item:Object in result)
dictionary[item] = true;

result = new Array();

for (var key:Object in dictionary)
result.push(key);

dictionary = null;

return result;
}

但是,这种方法不适用于复杂数组。

是否有一个众所周知的算法,或者是否有可能编写一个可以比较 Vector.<Object> 的递归函数?和另外一个?即使被比较的某些对象具有额外的键/值对,它也始终有效?


[编辑]


更清楚地说,使用字典来确定简单数组中的项目是否仅适用于原始数据类型(int、数字、字符串等)或对象引用,因此如果传递了 2 个类似于像这样:

var arr1:Array = new Array(1, 2, 3, 4, 5);
var arr2:Array = new Array(8, 7, 6, 5, 4);

生成具有以下值的合并数组:

1, 2, 3, 8, 7, 6, 5, 4

相比之下,我问的是是否可以传递一个函数 2 个复数数组或 Vector.<Object>所有包含可能具有相同键/值对的唯一对象,并删除结果中的冗余 Vector.<Object> .例如:

var vec1:Vector.<Object> = new Vector.<Object>();
vec1.push({city:"Montreal", country:"Canada"});
vec1.push({city:"Halifax", country:"Canada"});

var vec2:Vector.<Object> = new Vector.<Object>();
vec2.push({city:"Halifax", country:"Canada"});
vec2.push({city:"Toronto", country:"Canada"});

通过确定和删除具有相同键/值对的对象,合并上述 2 个向量对象将产生以下向量:

{city:"Montreal", country:"Canada"}
{city:"Halifax", country:"Canada"}
{city:"Toronto", country:"Canada"}

我正在寻找一种算法来处理这些相似对象的删除,而无需知道它们的特定键/值名称或对象中有多少键/值对。

最佳答案

当然可以,您可以使用任何类型的 Vector 构建一个类似的示例:

     public function mergeObjectVectors(v1:Vector.<Object>,                      
v2:Vector.<Object>):Vector.<Object>
{
var dictionary:Dictionary = new Dictionary();
var concat:Vector.<Object> = v1.concat(v2);
var result:Vector.<Object> = new Vector.<Object>();

for each(var i:Object in concat)
{
if (!dictionary[i])
{
dictionary[i] = true;
result.push(i);
}
}

return result;
}

但是,如果您打算接受任何类型的向量,那就不同了:

        public function testing():void
{
var v1:Vector.<Object> = new Vector.<Object>();
v1.push({name:"Object 1"});
v1.push({name:"Object 2"});

// Vector w duplicates
var v2:Vector.<Object> = new Vector.<Object>();
var o:Object = {name:"Object 3"};
v2.push(o);
v2.push(o);
v2.push(o);

var resultVector:Vector.<Object> = mergeAnything(v1, v2, Class(Vector.<Object>));
var resultArray:Array = mergeAnything(v1, v2, Array);
var resultObject:Object = mergeAnything(v1, v2, Object);
}


public function mergeAnything(o1:Object, o2:Object, resultClass:Class):*
{

var dictionary:Dictionary = new Dictionary();
var result:Object = new resultClass();

var i:int;
for each(var o:Object in o1)
{
if (!dictionary[o])
{
dictionary[o] = true;
result[i++] = o;
}
}

for each(o in o2)
{
if (!dictionary[o])
{
dictionary[o] = true;
result[i++] = o;
}
}

return result;
}

第一个例子会更节省资源。


编辑:这应该可以做到,用你的例子试试:

     public function mergeObjectVectors(v1:Vector.<Object>, v2:Vector.<Object>):Vector.<Object>
{
var concat:Vector.<Object> = v1.concat(v2);
var result:Vector.<Object> = new Vector.<Object>();

var n:int = concat.length;
loop:for (var i:int = 0; i < n; i++)
{
var objectToAdd:Object = concat[i];

var m:int = result.length;
for (var j:int = 0; j < m; j++)
{
var addedObject:Object = result[j];
if (this.areObjectsIdentical(objectToAdd, addedObject))
{
continue loop;
}
}
result.push(objectToAdd);
}

return result;
}

private function areObjectsIdentical(o1:Object, o2:Object):Boolean
{
var numComparisons:int = 0;

for (var s:String in o1)
{
numComparisons++;
if (o1[s] != o2[s])
{
return false;
}
}
for (s in o2)
{
numComparisons--;
}

return !numComparisons;
}

关于actionscript-3 - ActionScript - 比较和删除复杂数组的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com