c# 判断两个数组是否相等的方法
展开全部
using System;
using System.Reflection;
namespace DebugConsole
{
class Program
{
static void Main(string[] args)
{
string[] s1 = { "a", "bcd", "xx" };
string[] s2 = { "a", "bcd", "xx" };
string[] s3 = { "a", "bcd", "xx1" };
Console.WriteLine("s1 == s2 ---> {0}", ArrayEqals(s1, s2));
Console.WriteLine("s2 == s3 ---> {0}", ArrayEqals(s2, s3));
int[] n1 = { 1, 2, 3 };
int[] n2 = { 1, 2, 3 };
int[] n3 = { 2, 2, 3 };
Console.WriteLine("n1 == n2 ---> {0}", ArrayEqals(n1, n2));
Console.WriteLine("n2 == n3 ---> {0}", ArrayEqals(n2, n3));
Console.ReadKey();
}
public static bool ArrayEqals<T>(T[] array1, T[] array2)
{
if (array1 == array2) return true;
if (array1 == null || array2 == null) return false;
if (array1.Length != array1.Length) return false;
Type equatable = typeof(T).GetInterface("IEquatable`1");
if (equatable != null)
{
MethodInfo equalMethod = equatable.GetMethod( "Equals", BindingFlags.Instance | BindingFlags.Public );
for (int i = 0; i < array1.Length; i++)
{
if (!(bool)equalMethod.Invoke(array1[i], new object[] { array2[i] })) return false;
}
return true;
}
Type comparable = typeof(T).GetInterface("IComparable`1");
if (comparable != null)
{
MethodInfo compareMethod = comparable.GetMethod("CompareTo", BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < array1.Length; i++)
{
if (!(bool)compareMethod.Invoke(array1[i], new object[] { array2[i] })) return false;
}
return true;
}
for (int i = 0; i < array1.Length; i++)
{
if (!array1[i].Equals(array2[i])) return false;
}
return true;
}
}
}
using System.Reflection;
namespace DebugConsole
{
class Program
{
static void Main(string[] args)
{
string[] s1 = { "a", "bcd", "xx" };
string[] s2 = { "a", "bcd", "xx" };
string[] s3 = { "a", "bcd", "xx1" };
Console.WriteLine("s1 == s2 ---> {0}", ArrayEqals(s1, s2));
Console.WriteLine("s2 == s3 ---> {0}", ArrayEqals(s2, s3));
int[] n1 = { 1, 2, 3 };
int[] n2 = { 1, 2, 3 };
int[] n3 = { 2, 2, 3 };
Console.WriteLine("n1 == n2 ---> {0}", ArrayEqals(n1, n2));
Console.WriteLine("n2 == n3 ---> {0}", ArrayEqals(n2, n3));
Console.ReadKey();
}
public static bool ArrayEqals<T>(T[] array1, T[] array2)
{
if (array1 == array2) return true;
if (array1 == null || array2 == null) return false;
if (array1.Length != array1.Length) return false;
Type equatable = typeof(T).GetInterface("IEquatable`1");
if (equatable != null)
{
MethodInfo equalMethod = equatable.GetMethod( "Equals", BindingFlags.Instance | BindingFlags.Public );
for (int i = 0; i < array1.Length; i++)
{
if (!(bool)equalMethod.Invoke(array1[i], new object[] { array2[i] })) return false;
}
return true;
}
Type comparable = typeof(T).GetInterface("IComparable`1");
if (comparable != null)
{
MethodInfo compareMethod = comparable.GetMethod("CompareTo", BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < array1.Length; i++)
{
if (!(bool)compareMethod.Invoke(array1[i], new object[] { array2[i] })) return false;
}
return true;
}
for (int i = 0; i < array1.Length; i++)
{
if (!array1[i].Equals(array2[i])) return false;
}
return true;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用个循环语句
string a[];
string b[];
bool bo;
for(int i=0;i<n;i++)
{
if(a[i]==b[i])
bo=true;
else
{
bo=false;
break;
}
}
//得到的bo就能判断了
string a[];
string b[];
bool bo;
for(int i=0;i<n;i++)
{
if(a[i]==b[i])
bo=true;
else
{
bo=false;
break;
}
}
//得到的bo就能判断了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先判断它的长度是否相等,然后再遍历数组中的元素,看它是否相等
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一步,判断数组长度是否相等
第二步,使用foreach循环遍历,判断每一个元素是否相等
第二步,使用foreach循环遍历,判断每一个元素是否相等
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询