Environment: array A and array B
Int [] ArrayA = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Int [] ArrayB = new [] {9, 1, 4, 5, 2, 3, 6, 7, 8 };
We need to compare the content of the Array instead of the object reference of the Array. Therefore, we cannot take Array. Equals (ArrayA, ArrayB) (the result is false)
Now let's take a look at how to compare the array content. Of course you can compare it with loops. Here we will not talk about the method of loop comparison. Now let's look at other methods:
Method 1: Use the generic Method
Static bool ArraysEqual <T> (T [] a1, T [] a2) {if (ReferenceEquals (a1, a2) return true; if (a1 = null | a2 = null) return false; if (a1.Length! = A2.Length) return false; EqualityComparer <T> comparer = EqualityComparer <T>. Default; for (int I = 0; I <a1.Length; I ++) {if (! Comparer. Equals (a1 [I], a2 [I]) return false;} return true;} [html] view plaincopyprint? Static bool ArraysEqual <T> (T [] a1, T [] a2) {if (ReferenceEquals (a1, a2) return true; if (a1 = null | a2 = null) return false; if (a1.Length! = A2.Length) return false; EqualityComparer <T> comparer = EqualityComparer <T>. Default; for (int I = 0; I <a1.Length; I ++) {if (! Comparer. Equals (a1 [I], a2 [I]) return false;} return true;} static bool ArraysEqual <T> (T [] a1, T [] a2)
{
If (ReferenceEquals (a1, a2 ))
Return true;
If (a1 = null | a2 = null)
Return false;
If (a1.Length! = A2.Length)
Return false;
EqualityComparer <T> comparer = EqualityComparer <T>. Default;
For (int I = 0; I <a1.Length; I ++)
{
If (! Comparer. Equals (a1 [I], a2 [I]) return false;
}
Return true;
}
Method 2: Extended method of Enumerable. SequenceEqual: Determine whether the two sequences are equal. For more information, see msdn: http://msdn.microsoft.com/en-us/library/bb348567.aspx.
Bool equals = ArrayA. OrderBy (
=> A). SequenceEqual (ArrayB. OrderBy (a => ));
Method 3: Use the Linq method to query the number of data comparisons of the intersection.
Var q = from a in ArrayA join B in ArrayB on a equals B select a; bool limit S1 = ArrayA. length = ArrayB. length & q. count () = ArrayA. length;
[Html] view plaincopyprint? Var q = from a in ArrayA join B in ArrayB on a equals B select a; bool limit S1 = ArrayA. length = ArrayB. length & q. count () = ArrayA. length; var q = from a in ArrayA
Join B in ArrayB on a equals B
Select;
Bool defaults S1 = ArrayA. Length = ArrayB. Length & q. Count () = ArrayA. Length;
Method 4: ALL
Var set = new HashSet <byte> (ArrayA );
Bool allThere = ArrayB. All (set. Contains );
Method 5: use mathematical ideas to use intersection and Union ideas
Bool upload S3 = ArrayA. Intersect (ArrayB). Count () = ArrayA. Union (ArrayB). Count ();