Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取数组中重复元素的索引。。?_C#_Asp.net - Fatal编程技术网

C# 如何获取数组中重复元素的索引。。?

C# 如何获取数组中重复元素的索引。。?,c#,asp.net,C#,Asp.net,我试图找到数组中存储的元素的索引, 有些元素重复出现,当我试图得到这些元素的索引时,总是给出第一个元素的索引。 例如: 当我想把3的索引设为5时,仍然是0。 我不能跳过逻辑中的元素,我必须每次在程序中检查每个元素。 如果有的话,请帮忙 尊敬。您可以使用LINQ和选择(IEnumerable,Func)-以下是示例 然后使用LastOrDefault()来获取最后一个索引(如果这是您想要的),就像toarray一样,数组的索引有一个重载,该重载采用了起始索引。使用第一项的索引查找下一项: int[

我试图找到数组中存储的元素的索引, 有些元素重复出现,当我试图得到这些元素的索引时,总是给出第一个元素的索引。 例如:

当我想把3的索引设为5时,仍然是0。 我不能跳过逻辑中的元素,我必须每次在程序中检查每个元素。 如果有的话,请帮忙


尊敬。

您可以使用
LINQ
选择(IEnumerable,Func)
-以下是示例


然后使用
LastOrDefault()
来获取最后一个索引(如果这是您想要的),就像
toarray

一样,数组的
索引有一个重载,该重载采用了起始索引。使用第一项的索引查找下一项:

int[] arr = {3,5,6,7,2,3,11,14 };

int index = Array.IndexOf(arr, 3);
Console.WriteLine(index);

int index2 = Array.IndexOf(arr, 3, index + 1);
Console.WriteLine(index2);

Console.ReadLine();

我假设您正在搜索一个与搜索项在数组中出现的时间数无关的解决方案。在这种情况下,您需要一个循环,用于对找到的当前项执行工作

int[] arr = {3,5,6,7,2,3,11,14 };
int index = -1;
while((index = Array.IndexOf(arr, 3, index + 1)) != -1)
{
    Console.WriteLine(index);
}

重载将完全按照您的预期工作

请尝试以下操作:

int[] arrRepaetInd = new int[arr.Length];
int j=0,cnt=0;
for(int i=0;i<arr.Length;i++)
{
  j++;
  if(arr[i]==arr[i+1]
  {
    cnt++;
    arrRepaetInd[cnt]=arr[i];
    Console.WriteLine(arrRepaetInd[cnt]);
  }
}
int[]arrrapetind=新int[arr.Length];
int j=0,cnt=0;

对于(int i=0;i您希望使用Select加上一个过滤器

public int[] IndexesOf<T>(T[] Values, T find) {
    return Values.Select((i,index) => new { index = index, value = i})
                 .Where(x => x.value == find)
                 .Select(x => x.index)
                 .ToArray();
}

您可以拥有一个实现IEnumerable并返回所需索引的类:

public class Traverse<T> : IEnumerable<int>
{
    T[] _list;
    T _value;

    public Traverse(T[] list, T value)
    {
        this._list = list;
        this._value = value; 
    }

    public IEnumerator<int> GetEnumerator()
    {
        for (int i = 0; i < _list.Length; i++)
            if (_list[i].Equals(_value))
                yield return i;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
公共类遍历:IEnumerable
{
T[]_列表;
T_值;
公共导线测量(T[]列表,T值)
{
这个._list=list;
此值为._值=值;
}
公共IEnumerator GetEnumerator()
{
对于(int i=0;i<\u list.Length;i++)
如果(_列表[i]。等于(_值))
收益率i;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}
然后像这样使用它:

    int[] arr = { 3, 5, 6, 7, 2, 3, 11, 14 };

    foreach (var index in new Traverse<int>(arr, 3))
        Console.WriteLine(index.ToString());
int[]arr={3,5,6,7,2,3,11,14};
foreach(新导线中的var指数(arr,3))
Console.WriteLine(index.ToString());
输出: 0
5

你为什么想要这个indexes@AmitSingh这真的很重要吗?我需要在我的程序中根据数组元素的大小发送数组的索引,我不想交换元素,我只想发送数组元素的索引,首先我必须发送数组中最小元素的索引,然后是较大元素的索引,依此类推..我不想交换。请elp.我需要根据数组元素的大小在我的程序中发送数组的索引,我不想交换元素,我只想发送数组元素的索引,首先我必须发送数组中最小元素的索引,然后是较大元素的索引,依此类推..我不想交换。请帮助。@user2345759:交换从哪里来?代码是这样的esn不需要这样做。代码只需查找
3
的第一个实例,然后使用该实例的索引查找
3
的下一个实例。我需要根据数组元素大小发送程序中数组的索引,我不想交换元素,我只想发送数组元素的索引,首先我必须发送sma的索引我的数组中的llest元素,然后是更大的索引等等..我不想交换。请帮助。对于上面的代码,当搜索项为3时,索引变量在第一个循环中为0,在第二个循环中为5。你说的“发送数组元素的索引”是什么意思?我想发送元素的索引,int[]arr={3,5,6,7,2,3,11,14};对于此数组,我尝试返回..4,0,5,1,2,3,6,7
public static class MyExtensions {
    public static int[] IndexesOf<T>(this T[] Values, T find) {
        return Values.Select((i,index) => new { index = index, value = i})
                     .Where(x => x.value == find)
                     .Select(x => x.index)
                     .ToArray();
    }
}
var indexes = arr.IndexesOf(3);
public class Traverse<T> : IEnumerable<int>
{
    T[] _list;
    T _value;

    public Traverse(T[] list, T value)
    {
        this._list = list;
        this._value = value; 
    }

    public IEnumerator<int> GetEnumerator()
    {
        for (int i = 0; i < _list.Length; i++)
            if (_list[i].Equals(_value))
                yield return i;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
    int[] arr = { 3, 5, 6, 7, 2, 3, 11, 14 };

    foreach (var index in new Traverse<int>(arr, 3))
        Console.WriteLine(index.ToString());