查找数组中的值&它的索引。 [英] Finding the value in an array & its index.

查看:80
本文介绍了查找数组中的值&它的索引。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以告诉我如何在数组中查找具有相似元素或不同的数组的值及其在数组中的索引并将其与a进行比较包含其他数据的索引的数组。喜欢Ex:



在array1 [6] = {1,0,0,1,0,2,0}这里我需要检查数组是否包含1s并在数组中提取1s的索引。在这个例子中,我们可以看到数组包含1,它们的索引是0和4.我有另一个数组,其中包含其他数据的索引,即array2 [2] = {2,5}。所以在这里我需要将array1的array1与array2的数据进行比较,如果两者相同则会触发一些事件,如果不是其他事件则会触发。



谢谢提前。

Hi all,

Can anybody tell me how to look for value in an array with similar elements or different & its index in the array & compare it with a array containing indices''s of other data. Like for Ex:

In array1[6] = {1,0,0,1,0,2,0} here i need to check whether the array contains 1s in it & extract the index of 1s in the array. In this example we can see that the array contains 1s & their indices''s are 0 & 4. I have got another array which contains the indices''s of other data i.e., array2[2] = {2,5}. So here i need to compare indices''s of array1 with the data of array2 if both are same some event will be triggered if not some other event.

Thanks in advance.

推荐答案

IT并不难做,但加速它的正常方法不起作用 - 你不能使用Linq因为它适用于无序集,所以它没有索引的概念。但无论如何,蛮力和无知的方法非常简单:

IT''s not difficult to do, but the "normal" methods to speed it up don''t work - you can''t use Linq because it works on unordered sets, so it has no concept of "index". But the "brute force and ignorance" approach is pretty simple anyway:
int[] myArray = { 1, 0, 0, 1, 0, 2, 0 };
int[] match = { 2, 5 };
List<int> indexes = new List<int>();
for (int i = 0; i < myArray.Length; i++)
    {
    if (myArray[i] == 1) indexes.Add(i);
    }
if (Enumerable.SequenceEqual(indexes, match))
    {
    Console.WriteLine("They match");
    }


上个世纪的阵列太棒......



但是说真的,您可能希望将索引放在 HashSet [ ^ ]然后对它们执行设置操作。
Arrays are sooo last century...

But seriously, you might want to put the indices in HashSet[^]s and then perform set operations on them.


这篇关于查找数组中的值&amp;它的索引。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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