그래서 동일한 값의 배열이 스택 내에 있는지 확인하려고합니다. 그대로 여기 배열이 스택에 있는지 확인하는 방법은 무엇입니까?
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가 전체 스택에서 string 유형의 값을 찾을 수 있다고 생각하면 int [] 유형의 값에 대해 가능해야한다는 결론이 나에게 남습니다. 그렇지 않은 경우 가장 좋은 해결 방법에 대한 지침을 알려 주시면 감사하겠습니다.
** 스택의 ** 요소가 ** 특정 값에 대해 ** 동일하게 ** 있는지 알고 싶습니다. 그게 당신이 그것을 구현하는 방법에 대한 아이디어를 제공합니까? –
Contains()를 사용하려면 개체 ID 만 확인하는 기본값보다 많은 작업을 수행하는 IEqualityComparer가 필요합니다. –