2017-05-03 39 views
0

在已经创建了一个简化的查找表,阵列值,该值是真或假用于查找

对于阵列样品(查找表),我想值设定为真或假。用户将输入响应数组。程序然后将数组与样本进行比较以获得序列相等性。任何想法如何我可以做到这一点。

//Note code has been simplified 

// Array for look up 
bool [] firstArray = new bool []{true,false| true}; 


//.................... 


//array for response 
bool [] sampl = new bool[] {true,false}; 

if(sample.SequenceEqual(sampl)) 
{ 
    Console.WriteLine("There are equal"); 

//Output should be true 
} 
+2

'如果(sample.SequenceEqual(SAMPL))'这里是'sample'的定义是什么? – fubo

+0

@fubo检查'array for response'后的行 – Michael

+1

请更正您的问题并添加更多详细信息 –

回答

0

有很多方法可以做到这一点。一种方法是遍历两个数组,并对每个数组的值进行投影。下面的代码将通过两个阵列运行,并通过各指标在数组中比较项目的值存储bool

var zipped = firstArray.Zip(sampl, (a, b) => (a == b)); 

现在,我们可以检查是否有属于不同的项目。

var hasDiiff = zipped.Any(x=> x == false); 

请注意,如果你的阵列不具有相同的长度,当第一个结束Zip将停止。

你可以做整个事情在一个行,如果你想:

var hasDiff = first array.Zip(sampl, (a, b) => (a == b)) 
     .Any(x=> x == false); 

见我的回答here如何Zip作品进行了深入的解释。

0

false| true值是true所以你的firstArray定义,其实这相当于:

bool [] firstArray = new bool []{true, true}; 

你需要做的是建立一套规则,你匹配:

Func<bool, bool>[] rules = new Func<bool, bool>[] { x => x == true, x => true }; 

然后,你可以这样做:

bool[] sampl = new bool[] { true, false }; 

if (rules.Zip(sampl, (r, s) => r(s)).All(x => x)) 
{ 
    Console.WriteLine("There are equal"); 

    //Output should be true 
} 

如果你想让它读取更容易一些,你可以这样做:

Func<bool, bool> trueOnly = x => x == true; 
Func<bool, bool> falseOnly = x => x == false; 
Func<bool, bool> trueOrFalse = x => true; 

Func<bool, bool>[] rules = new Func<bool, bool>[] { trueOnly, trueOrFalse };