比较两个不同大小的数组 [英] Comparing two different sized arrays
本文介绍了比较两个不同大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个字符串数组,一个比另一个大,并且我需要检查数组中的任何字符串是否相同.
I have two string arrays, one bigger than the other, and i need to check if any of the strings in the arrays are the same.
推荐答案
My首选方法是对每个数组进行排序.
然后只需比较一个循环:
My preferred method would be to sort each array.
Then just compare with a single loop:
If (string A > string B)
move B
else if (string A < string B)
move A
else
Same - record, and move A and B.
您可以使用Array.Sort将字符串数组按顺序排序:
You can use Array.Sort to sort string arrays into order:
string[] st = new string[] { "qwertyuiop", "asdfghjkl", "zxcvbnm" };
Array.Sort(st);
尝试一下:
Try this:
string[] array1, array2;
//list to store index's of same strings
List<keyvaluepair><int,int>> couplesList = new List<keyvaluepair><int,int>>();
//array creation goes here
//the algorithm
for(int i = 0; i < array1.Length; i++)
{
for(int j = 0; j < array2.Length; j++)
{
if(array1[i] == array2[j])
{
couplesList.Add(new KeyValuePair<int,int>(i,j));
}
}
}</keyvaluepair></keyvaluepair>
我已经做了类似的事情:
I wud have done something like this:
//smaller is the array with less no. of strings
//bigger is the array with more no. of strings
//this loop compares if any string in smaller is present in bigger
foreach(string i in smaller)
foreach(string j in bigger)
{
if(i==j)
Console.WriteLIne(i);//print the common string
}
这篇关于比较两个不同大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文