此问题在此处已有答案:
How to get a distinct result for list of array?(4个答案)
上个月关门了。
我试图找到两个数组中的成员,它们出现在一个数组中,但在另一个数组中却没有。我读过this article,我想虽然我理解了它,但我做错了一些事情。
假设以下简单代码:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[][] currentLocation = new int[][]
{
new int[] { 11, 10 },
new int[] { 11, 11 },
new int[] { 11, 12 },
new int[] { 11, 13 },
};
int[][] proposedLocation = new int[][]
{
new int[] { 11, 12 },
new int[] { 11, 13 },
new int[] { 11, 14 },
new int[] { 11, 15 },
};
IEnumerable<int[]> onlyInFirstSet = proposedLocation.Except(currentLocation);
foreach (int[] number in onlyInFirstSet)
Console.WriteLine(number[0].ToString() + ","+number[1].ToString());
}
}
现在我希望/想到/期望看到的是:
11,14
11,15
......因为在proposedLocation
中,这些是唯一没有出现在currentLocation
中的,但是我实际上得到了:
11,12
11,13
11,14
11,15
(i.e.整个proposedLocation
)
**问题:**如何提取只出现在proposedLocation
中的值?如果我能以某种方式保留int[][]
而不是string
的奇数位,我将是有益的,但这是一个额外的好处。
2条答案
按热度按时间pb3s4cty1#
Using an
int[] { 11, 10}
for a location isn't sufficient for a few reasons.This can cause bugs as locations you think have specific values, due to a programming error, can easily change locations.
Console.WriteLine(array)
because it won't display the values, and thus you need a loop, or usestring.Join()
to create a displayable array.Location as a struct (X,Y)
All of the above, combined, call for a different approach. I suggest using a structure that can store two values (just as the array), only two values, no more or less, can check for equality with other locations, and finally has a built-in way to convert to a string for display.
Some key features of this structure are
X
,Y
properties hold the values and are unchanging. A location is defined by its (x,y) values and they go in pairs together. The values are set at the constructor and are readonly.Equals(Location other)
check for value equality (both x and y).ToString()
automatically converts the structure to a string when needed. This allows calls likeConsole.WriteLine(location)
to produce the results(11,14)
for example.Equals()
function as we forward the genericEquals(object)
to the specificEquals(Location)
function.GetHashCode()
function you can ignore for now. It is there because it is required by certain collections, and it guarantees that if two locations are different, that they will have different hash-codes.Program
Now to test for the use case described in the question
with output as expected:
Note that due to the operation of
Except()
and the definition provided, you need to subtract the fist list from the second list, and that is way I am calling.Except(proposedLocation, currentLocation)
and not the other way around.Location as a tuple (int,int)
You can achieve the same result, by using a
Tuple
of twoint
values. Actually aValueTuple<int,int>
would have the exact same behavior as our custom structureLocation
above.The code below has the exact same result as before, does not require a custom structure, and keeps type safety , as opposed to a solution that casts locations as
object
.ecfsfe2w2#
同意-MySkullCaveIsADarkPlace。但是,如果您想使用解决方法来达成此目的,请参阅下列。
请使用对象,而不要使用不规则数组。