当元素顺序无关紧要时,比较两个数组 [英] Compaing two arrays when element order does not matter

查看:83
本文介绍了当元素顺序无关紧要时,比较两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个定义为string []的字符串数组,这样如果两者的内容相同,那么

两个数组是相等的,其中

命令没关系,每个元素都必须是唯一的。


例如这两个数组的测试结果相同:


servers [0] =" Admin"

servers [1] =" Finance"

servers [2] =" Payroll"

servers [3] =" Sales"

monitor [0] =" Sales" ;

监视器[1] =工资单

监视器[2] =" Admin"

监视器[3] ="财务


有没有一种简单的方法可以做到这一点?我能想到的唯一方法是:

1.检查元素的数量是否相同,因为如果不是那么

它们不相等,因为每个元素都必须是唯一的。

2.创建另一个数组(第一个

数组中的每个元素都有一个元素)并为
首先检查它是否存在并在附加数组中将其标记为

确实如此。

坦率地说,这看起来很笨拙。有一个更好的方式

解决方案
在2006年2月2日5时54分48秒-0800," ssg31415926" < ne ********** @ gmail.com>

写道:

我需要比较定义为字符串的两个字符串数组[]如果两个数组的内容相同,则两个数组是相等的,其中
顺序无关紧要,每个元素必须是唯一的。

例如这两个阵列的测试结果相同:

servers [0] =" Admin"
servers [1] =" Finance"
servers [2] =" Payroll" ;
servers [3] =" Sales"

monitor [0] =" Sales"
monitor [1] =" Payroll"
监视器[ 2] =" Admin"
监视器[3] =财务

有没有简单的方法可以做到这一点?我能想到的唯一方法是:
1。检查元素的数量是否相同,因为如果没有,则它们不相等,因为每个元素必须是唯一的。
2。创建另一个数组(第一个
数组中的每个元素都有一个元素),然后首先为第一个元素运行第二个数组,检查它是否存在并在附加数组中将其标记为<它确实如此。
坦率地说,这看起来很笨拙。有更好的方法吗?



这两个数组都必须是数组吗?如果一个可以是Hashtable,那么你可以加载它并用它来测试数组。


或者如果它们必须是数组你可以使用一个二元搜索算法

搜索其中一个,但其中一个必须进行排序才能做到这一点。


Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com




" ssg31415926" <是ne ********** @ gmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

我需要比较两个定义为string []的字符串数组,这样两个数组相等,如果两者的内容相同,那么
顺序并不重要必须是独一无二的。

例如这两个阵列的测试结果相同:

servers [0] =" Admin"
servers [1] =" Finance"
servers [2] =" Payroll" ;
servers [3] =" Sales"

monitor [0] =" Sales"
monitor [1] =" Payroll"
监视器[ 2] =" Admin"
监视器[3] =财务

有没有简单的方法可以做到这一点?我能想到的唯一方法是:
1.检查元素的数量是否相同,因为如果不是那么它们就不相等,因为每个元素都必须是唯一的。
2 。创建另一个数组(第一个
数组中的每个元素都有一个元素),然后首先对第一个元素运行第二个数组,检查它是否存在并在附加数组中标记它
确实如此。
坦率地说,这看起来很笨拙。有更好的方法吗?




进行长度检查然后对第一个数组进行排序,然后使用二进制搜索来查看第二个元素的元素第一个数组。


只需将数组元素复制到哈希表或排序列表中即可对它们进行排序。

一旦排序,你可以迭代排序列表并进行比较。


再次,正如我经常建议的那样,考虑使用基于CollectionBase的自定义

集合而不是数组,您可以对您的集合进行排序没有

必须将你的阵列复制到其他任何东西。


HTH

-

戴尔普雷斯顿

MCAD C#

MCSE,MCDBA

" ssg31415926"写道:

我需要比较定义为string []的两个字符串数组,这样如果两个数组的内容相同,则两个数组相等,其中
顺序没关系,每个元素都必须是唯一的。

例如这两个阵列的测试结果相同:

servers [0] =" Admin"
servers [1] =" Finance"
servers [2] =" Payroll" ;
servers [3] =" Sales"

monitor [0] =" Sales"
monitor [1] =" Payroll"
监视器[ 2] =" Admin"
监视器[3] =财务

有没有简单的方法可以做到这一点?我能想到的唯一方法是:
1.检查元素的数量是否相同,因为如果不是那么它们就不相等,因为每个元素都必须是唯一的。
2 。创建另一个数组(第一个
数组中的每个元素都有一个元素),然后首先对第一个元素运行第二个数组,检查它是否存在并在附加数组中标记它
确实如此。
坦率地说,这看起来很笨拙。有更好的方法吗?



I need to compare two string arrays defined as string[] such that the
two arrays are equal if the contents of the two are the same, where
order doesn''t matter and every element must be unique.

E.g. these two arrays would test as equal:

servers[0] = "Admin"
servers[1] = "Finance"
servers[2] = "Payroll"
servers[3] = "Sales"

monitors[0] = "Sales"
monitors[1] = "Payroll"
monitors[2] = "Admin"
monitors[3] = "Finance"

Is there an easy way to do this? The only way I can think of is:
1. check that the number of elements is the same because if not then
they''re not equal since every element must be unique.
2. create another array (with one element for each element in the first
array) and run through the second array once for each element in the
first, checking if it exists and marking it in the additional array if
it does.
Frankly, this seems clumsy. Is there a better way?

解决方案

On 2 Feb 2006 05:54:48 -0800, "ssg31415926" <ne**********@gmail.com>
wrote:

I need to compare two string arrays defined as string[] such that the
two arrays are equal if the contents of the two are the same, where
order doesn''t matter and every element must be unique.

E.g. these two arrays would test as equal:

servers[0] = "Admin"
servers[1] = "Finance"
servers[2] = "Payroll"
servers[3] = "Sales"

monitors[0] = "Sales"
monitors[1] = "Payroll"
monitors[2] = "Admin"
monitors[3] = "Finance"

Is there an easy way to do this? The only way I can think of is:
1. check that the number of elements is the same because if not then
they''re not equal since every element must be unique.
2. create another array (with one element for each element in the first
array) and run through the second array once for each element in the
first, checking if it exists and marking it in the additional array if
it does.
Frankly, this seems clumsy. Is there a better way?


Do the two arrays both have to be arrays? If one can be a Hashtable,
then you can load it and use it to test the array.

Or if they have to be arrays you can use a binary search algorithm to
search one of them but one of them will have to be sorted to do that.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com



"ssg31415926" <ne**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

I need to compare two string arrays defined as string[] such that the
two arrays are equal if the contents of the two are the same, where
order doesn''t matter and every element must be unique.

E.g. these two arrays would test as equal:

servers[0] = "Admin"
servers[1] = "Finance"
servers[2] = "Payroll"
servers[3] = "Sales"

monitors[0] = "Sales"
monitors[1] = "Payroll"
monitors[2] = "Admin"
monitors[3] = "Finance"

Is there an easy way to do this? The only way I can think of is:
1. check that the number of elements is the same because if not then
they''re not equal since every element must be unique.
2. create another array (with one element for each element in the first
array) and run through the second array once for each element in the
first, checking if it exists and marking it in the additional array if
it does.
Frankly, this seems clumsy. Is there a better way?



Do the length check then sort the first array then use binary search to look
for elements of the second array in the first.


Just copy the array elements to a hashtable or sorted list to sort them.
Once sorted, you can iterate through the sorted list and compare.

Again, as I often suggest, consider using a CollectionBase based custom
collection instead of an array and you can sort your collection without
having to copy your array to anything else.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
"ssg31415926" wrote:

I need to compare two string arrays defined as string[] such that the
two arrays are equal if the contents of the two are the same, where
order doesn''t matter and every element must be unique.

E.g. these two arrays would test as equal:

servers[0] = "Admin"
servers[1] = "Finance"
servers[2] = "Payroll"
servers[3] = "Sales"

monitors[0] = "Sales"
monitors[1] = "Payroll"
monitors[2] = "Admin"
monitors[3] = "Finance"

Is there an easy way to do this? The only way I can think of is:
1. check that the number of elements is the same because if not then
they''re not equal since every element must be unique.
2. create another array (with one element for each element in the first
array) and run through the second array once for each element in the
first, checking if it exists and marking it in the additional array if
it does.
Frankly, this seems clumsy. Is there a better way?



这篇关于当元素顺序无关紧要时,比较两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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