如何判断数组是否在栈中?
How to determine if an array is in a stack?
所以我试图确定一个相同值的数组是否在堆栈中。
Stack<string> stackOfStrings = new Stack<string>();
stackOfStrings.Push("dog");
stackOfStrings.Push("cat");
Debug.WriteLine("stackOfString contains cat: " +
stackOfStrings.Contains("cat"));
Debug.WriteLine("stackOfString contains dog: " +
stackOfStrings.Contains("dog"));
Stack<int[]> stackOfIntArrays = new Stack<int[]>();
stackOfIntArrays.Push(new int[]{0,1});
stackOfIntArrays.Push(new int[]{1,1});
int[] array01 = new int[]{0,1};
int[] anotherArray01 = new int[]{0,1};
int[] array11 = new int[] { 1, 1 };
Debug.WriteLine("SequenceEqual can compare arrays of identical value: " + array01.SequenceEqual(anotherArray01));
Debug.WriteLine("SequenceEqual can be used to compare the top value of a stack: " + stackOfIntArrays.Peek().SequenceEqual(array11));
//I want the line below to evaluate to true
Debug.WriteLine("Contains can be used to determine if an array of values is within the stack: " + stackOfIntArrays.Contains(array01));
下面是代码的输出:
stackOfString contains cat: True
stackOfString contains dog: True
SequenceEqual can compare arrays of identical value: True
SequenceEqual can be used to compare the top value of a stack: True
Contains can be used to determine if an array of values is within the stack: False
我对此的研究已经得出了很多结果,用于查看一个数组是在另一个数组中还是另一个数组的子集,这就是我了解 SequenceEqual 的方式。看到这似乎只能检查堆栈中的最高值,但它不足以满足我的需求。
我考虑过的另一种解决方案是将堆栈重新堆叠到新堆栈,并在移动时检查每个数组。然而,考虑到 contains 可以在整个堆栈中查找字符串类型的值,这让我得出结论,int[] 类型的值应该是可能的。如果没有,我们将不胜感激有关最佳解决方法的指导。
使用 LINQ 的 Any
而不是 Contains
对非等式的条件进行包含检查:
Debug.WriteLine(
"Enumerable.Any can be used to determine if an array of values is within the stack: {0}"
, stackOfIntArrays.Any(a => array01. SequenceEqual(a))
);
这相当于 "contains if ..." 任意条件的实现。
所以我试图确定一个相同值的数组是否在堆栈中。
Stack<string> stackOfStrings = new Stack<string>();
stackOfStrings.Push("dog");
stackOfStrings.Push("cat");
Debug.WriteLine("stackOfString contains cat: " +
stackOfStrings.Contains("cat"));
Debug.WriteLine("stackOfString contains dog: " +
stackOfStrings.Contains("dog"));
Stack<int[]> stackOfIntArrays = new Stack<int[]>();
stackOfIntArrays.Push(new int[]{0,1});
stackOfIntArrays.Push(new int[]{1,1});
int[] array01 = new int[]{0,1};
int[] anotherArray01 = new int[]{0,1};
int[] array11 = new int[] { 1, 1 };
Debug.WriteLine("SequenceEqual can compare arrays of identical value: " + array01.SequenceEqual(anotherArray01));
Debug.WriteLine("SequenceEqual can be used to compare the top value of a stack: " + stackOfIntArrays.Peek().SequenceEqual(array11));
//I want the line below to evaluate to true
Debug.WriteLine("Contains can be used to determine if an array of values is within the stack: " + stackOfIntArrays.Contains(array01));
下面是代码的输出:
stackOfString contains cat: True
stackOfString contains dog: True
SequenceEqual can compare arrays of identical value: True
SequenceEqual can be used to compare the top value of a stack: True
Contains can be used to determine if an array of values is within the stack: False
我对此的研究已经得出了很多结果,用于查看一个数组是在另一个数组中还是另一个数组的子集,这就是我了解 SequenceEqual 的方式。看到这似乎只能检查堆栈中的最高值,但它不足以满足我的需求。
我考虑过的另一种解决方案是将堆栈重新堆叠到新堆栈,并在移动时检查每个数组。然而,考虑到 contains 可以在整个堆栈中查找字符串类型的值,这让我得出结论,int[] 类型的值应该是可能的。如果没有,我们将不胜感激有关最佳解决方法的指导。
使用 LINQ 的 Any
而不是 Contains
对非等式的条件进行包含检查:
Debug.WriteLine(
"Enumerable.Any can be used to determine if an array of values is within the stack: {0}"
, stackOfIntArrays.Any(a => array01. SequenceEqual(a))
);
这相当于 "contains if ..." 任意条件的实现。