比较数组(听起来很简单,但我找不到它!) [英] Comparing Arrays (Sounds easy but I can't find it!)

查看:57
本文介绍了比较数组(听起来很简单,但我找不到它!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我有两个数组A和B.两个数组都是字节数组,每个数组有7个字节。

数组A的内容和B是相同的


A = {1,2,3,4,5,6,7};

B = {1,2,3 ,4,5,6,7};


当我这样做时


if(A == B)

$

dosomething();

}


系统不认为它们是相同的......


这与堆和堆栈有关吗? (*颤抖*)


如何比较两个阵列?


感谢您的帮助!


Mike

Hi Everyone!

I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each.
The contents of array A and B are the same

A = {1, 2, 3, 4, 5, 6, 7};
B = {1, 2, 3, 4, 5, 6, 7};

When I do

if (A == B)
{
dosomething();
}

the system does not think that they are the same...

Does this have something to do with heap and stack? (*shiver*)

How can I compare two arrays?

Thanks for your help!

Mike

推荐答案

好的。对于预定义的值类型,如果

其操作数的值相等,则相等运算符(==)返回true,否则返回false。对于_reference_类型

除了字符串,==返回true,如果它的两个操作数引用相同的

_object_。对于字符串类型,==比较字符串的值...


问题仍然存在,如何比较数组?
OK. For predefined value types, the equality operator (==) returns true if
the values of its operands are equal, false otherwise. For _reference_ types
other than string, == returns true, if its two operands refer to the same
_object_. For the string type, == compares the values of the strings...

Still the question remains, how does one compare arrays?

A == B正在测试对象引用A是否与对象引用相同

B.由于它们是不同的数组,它们将是不相等的。


A和B都是Array类型的对象。


据我所知,比较两个数组的唯一方法就是遍历

这两个数组直到你找到一个'unqeual'的元素。如果你在没有发现这种情况的情况下获得了所有的方式,那么它们就是相等的。你的意思是。


类似的东西:


public bool ArraysEqual(Array a1,Array a2){


if(a1.Length!= a2.Length){

返回false;

}


foreach(a1中的int i1){


foreach(int i2 in a2){


if(i1!= i2){

返回false;

}


}


}


返回true;

}


请注意,上面的快速和脏(和未经测试)例程假设你是

处理两个整数数组。这可能是为什么MSFT

没有将数组的等于运算符覆盖到更普遍的有用的东西的根源。如果没有泛型(将在下一个版本的
..NET中提供),很难实现一个有效的例程来比较数组

无论类型如何它们包含的数据。


--BOB


" Mike Bartels" <是ne ** @ news.t-online.de>在消息中写道

news:c8 ************* @ news.t-online.com ...
A == B is testing whether object reference A is the same as object reference
B. Since they are different arrays they will be unequal.

A and B are both objects of type Array.

To my knowledge the only way to compare two arrays is to iterate through
them both until you find an element that''s unqeual. If you get all the way
through without finding such, they are "equal" in the sense you mean.

Something like:

public bool ArraysEqual(Array a1,Array a2) {

if (a1.Length != a2.Length) {
return false;
}

foreach (int i1 in a1) {

foreach (int i2 in a2) {

if (i1 != i2) {
return false;
}

}

}

return true;
}

Notice that the quick and dirty (and untested) routine above assumes you''re
dealing with two integer arrays. This probably gets to the root of why MSFT
hasn''t overridden the equals operator for Array to something more generally
useful. Without generics (which will be available in the next release of
..NET) it''s difficult to implement an efficient routine to compare arrays
regardless of the type of data they contain.

--Bob

"Mike Bartels" <ne**@news.t-online.de> wrote in message
news:c8*************@news.t-online.com...
大家好! br />
我有两个数组A和B.两个数组都是字节数组,每个数组有7个字节。
数组A和B的内容是相同的

A = { 1,2,3,4,5,6,7};
B = {1,2,3,4,5,6,7};

当我做的时候/>
if(A == B)
{
dosomething();
}

系统不认为它们是相同的。 ..

这是否与堆和堆栈有关? (*颤抖*)

如何比较两个阵列?

感谢您的帮助!

Mike
Hi Everyone!

I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each.
The contents of array A and B are the same

A = {1, 2, 3, 4, 5, 6, 7};
B = {1, 2, 3, 4, 5, 6, 7};

When I do

if (A == B)
{
dosomething();
}

the system does not think that they are the same...

Does this have something to do with heap and stack? (*shiver*)

How can I compare two arrays?

Thanks for your help!

Mike



Mike Bartels< ne ** @ news.t-online.de>写道:
Mike Bartels <ne**@news.t-online.de> wrote:
好的。对于预定义的值类型,如果
的操作数值相等,则相等运算符(==)返回true,否则返回false。对于_reference_类型
除了string,==返回true,如果它的两个操作数引用相同的
_object_。对于字符串类型,==比较字符串的值...


请注意,它不仅仅是字符串 - 它是覆盖== <的任何类型br />
运营商。这可能包括你自己的一些类型,并且很可能在框架中覆盖等式运算符的其他类型。

问题仍然存在,如何进行比较数组?
OK. For predefined value types, the equality operator (==) returns true if
the values of its operands are equal, false otherwise. For _reference_ types
other than string, == returns true, if its two operands refer to the same
_object_. For the string type, == compares the values of the strings...
Note that it''s not just string - it''s any type which overrides the ==
operator. This could include some of your own types, and there may well
be other types in the framework which override the equality operator.
Still the question remains, how does one compare arrays?




编写一个比较

转换中每个元素的方法相当容易...


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



It''s fairly easy to write a method which compares each element in
turn...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于比较数组(听起来很简单,但我找不到它!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆