我只会以常见的方式比较元素。唯一的问题是我们应该如何对待第二种情况的“鱼”。
这是我针对两种情况的解决方案。1 - 如果我们不关心重复的项目,2 - 如果我们关心。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
private var arrOne:Array = ["fish", "cat", "dog", "tree", "frog"];
private var arrTwo:Array = ["cat", "cat", "fish", "dog", "fish"];
protected function compare():void
{
var rule1:int = 0;
var rule2:int = 0;
for (var i: int = 0; i < arrOne.length; i++)
{
for (var j: int = 0; j < arrTwo.length; j++)
{
if (arrOne[i] == arrTwo[j])
{
if (i == j)
rule1 ++;
else
rule2 ++;
}
}
}
Alert.show("You got " + rule1.toString() + " correct and in the right spot, and " + rule2.toString() + " correct but in the wrong spot");
}
protected function compareWithoutRepeat():void
{
var rule1:int = 0;
var rule2:int = 0;
var temp:Array = new Array();
for (var i: int = 0; i < arrOne.length; i++)
{
for (var j: int = 0; j < arrTwo.length; j++)
{
if (arrOne[i] == arrTwo[j])
{
if (i == j)
rule1 ++;
else
{
var flag:Boolean = false; //is there the same word in the past?
for (var k:int = 0; k < temp.length; k++)
{
if (temp[k] == arrTwo[j])
{
flag = true;
break;
}
}
if (!flag) //the word is new, count it!
{
rule2 ++;
temp.push(arrTwo[j]);
}
}
}
}
}
Alert.show("You got " + rule1.toString() + " correct and in the right spot, and " + rule2.toString() + " correct but in the wrong spot");
}
]]>
</fx:Script>
<s:Button x="10" y="10" width="153" label="Compare" click="compare()"/>
<s:Button x="10" y="39" label="Compare without repeat" click="compareWithoutRepeat()"/>
</s:Application>