如何获取数组元素并检入另一个数组 [英] How to take array element and check in another another arrays

查看:91
本文介绍了如何获取数组元素并检入另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

arr1 = wavFiles.ToArray();

arr2 = BaseDocs.ToArray();

arr3 = TemplateDocs.ToArray();

arr4 = DraftDocs.ToArray();



我有四个数组我需要取第一个数组中的元素并检查它是否在第二个,第三个和第四个数组元素如果存在我需要存储在字符串变量中我该怎么做



我尝试过:



我无法怎么做

arr1 = wavFiles.ToArray();
arr2 = BaseDocs.ToArray();
arr3 = TemplateDocs.ToArray();
arr4 = DraftDocs.ToArray();

I have four arrays i need to take the elements in the first array and check whether it is in second,third and fourth array elements if they exists i need to store in string variables how can i do this

What I have tried:

I could not get how to do this

推荐答案

你可以使用LINQ方法Intersect [ ^ ]轻松找到两个数组中的元素:

You can use the LINQ method Intersect[^] to easily find elements in both arrays:
var inBothArr1AndArr2 = arr1.Intersect(arr2);
var inBothArr1AndArr3 = arr1.Intersect(arr3);
var inBothArr1AndArr4 = arr1.Intersect(arr4);

现在你有三个 IEnumerable< T> 秒。用你的内容做你需要做的事。







从你的评论中,它看起来你想一个接一个地做这件事。这也是可能的:

Now you have three IEnumerable<T>s. Do with their contents what you need to do.



From your comment, it appears you want to do this one-by-one. That's possible too:

for (int i = 0; i < arr1.Length; i++)
{
    if (arr2.Contains(arr1[i]))
    {
        // Array 2 contains this element. Index: i. Value: arr1[i]
    }
    // do the same for arr3 and arr4
}


这篇关于如何获取数组元素并检入另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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