【问题标题】:getting the index of an array, multiple times, then storing them in an array c#多次获取数组的索引,然后将它们存储在数组中c#
【发布时间】:2015-06-05 02:45:54
【问题描述】:

我想得到一个我用Array.IndexOf(array, value) 完成的数组的索引。这适用于一个值,但我想获取该值的每次出现并将索引存储到另一个数组中。例如,名字'tom'在一个数组中出现5次,我想找到每一个的索引位置。

【问题讨论】:

  • 这是一个任务,而不是一个问题。你试过什么?你遇到过什么问题?
  • (您可能希望将 LINQ 与 Where 一起使用,顺便说一句...)
  • @JonSkeet 如果他想要索引,他不会(技术上他可以,正如 Habib 所写,但这是一个相当大的弯路)
  • 老实说,我不认为这是绕道而行。它准确地说明了需要什么......
  • 不,Ben 的代码(减去Count() 加上Length)是我们所需要的... 简单、实用、快速且适合初级程序员阅读。使用 .Select() 的半隐蔽重载来创建 n 个临时对象只是为了获得它们的索引更像是使用大炮杀死一只蚂蚁。 LINQ 不应该是所有问题的解决方案

标签: c# arrays indexing indexof


【解决方案1】:

也许是这样的?这使用列表而不是数组,但它遵循相同的想法。

List<int> Indexes = new List<int>();

for (int i = 0; i < array.Count(); i++)
{
    if (array[i] == "tom")
    {
        Indexes.Add(i);
    }
}

【讨论】:

  • 你应该使用.Length而不是.Count()
【解决方案2】:

此解决方案与上一个解决方案类似,但运行速度更快:

string value = "tom";
int[] indices = stringArray.Where(s => s != null)
                           .Select((s, i) => s.Equals(value) ? i: -1)
                           .Where(i => i >= 0)
                           .ToArray();

【讨论】:

    【解决方案3】:

    如果我没记错的话,您可以向IndexOf() 添加另一个参数,它可以让您指定从数组中的哪个位置开始。这应该或多或少地为您提供所需的东西:

    var indices = new List<int>();
    int i = Array.IndexOf(array, val);
    while(i > -1){
        indices.Add(i);
        i = Array.IndexOf(array, val, i+1);
    } 
    
    // ... and if it is important to have the result as an array:
    int[] result = indices.ToArray();
    

    实际例子:

    var array = new int[]{ 1, 2, 3, 3, 4, 5, 3, 6, 7, 8, 3};
    int val = 3;
    
    var indices = new List<int>();    
    int i = Array.IndexOf(array, val);
    while(i > -1){
        indices.Add(i);
        i = Array.IndexOf(array, val, i+1);
    }
    
    // ... and if it is important to have the result as an array:
    int[] result = indices.ToArray();
    

    编辑:刚刚意识到一个while循环可能比for循环干净得多。


    编辑 2: 由于大众需求(见下方评论),这里是原始的漂亮的非基本 for 循环,重新引入仅供您阅读快乐:

    for(int i = Array.IndexOf(array, val); i > -1; i = Array.IndexOf(array, val, i+1)){
        indices.Add(i);        
    } 
    

    【讨论】:

    • 但是...但是...非基本的for 太棒了...这是我在过去六个月中见过的最美丽的for 之一... . 我只为它投票给你:-)
    • @xanatos 谢谢,我感到很荣幸。事实上,我被你的评论深深打动了,我决定重新引入原来的 for 循环。享受! :)
    【解决方案4】:

    可以创建一个扩展方法来做到这一点

    namespace Extensions
    {
        public static class ArrayExtension
        {
            public static IEnumerable<int> GetIndicesOf<T>(this T[] target, T val, int start = 0)
            {
                EqualityComparer<T> comparer = EqualityComparer<T>.Default;
                for (int i = start; i < target.Length; i++)
                {
                    if (comparer.Equals(target[i], val))
                    {
                        yield return i;
                    }
                }
            }
        }
    }
    

    在您要调用它的文件中添加带有扩展方法using Extensions; 的命名空间的 using 语句。

    然后调用它只需执行以下操作以获取索引。

    IEnumerable<int> indices = array.GetIndicesOf(value);
    

    或者得到一个数组就行了

    int[] indicies = array.GetIndicesOf(value).ToArray();
    

    【讨论】:

      【解决方案5】:

      您可以使用 LINQ 的 Select 重载,它也使用元素索引,例如:

      var indices = stringArray.Select((s, i) => new {Value = s, Index = i})
          .Where(r => r.Value == "tom")
          .Select(r => r.Index);
      

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 2016-07-13
        • 1970-01-01
        • 2017-06-21
        • 2021-05-28
        • 2010-10-30
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        相关资源
        最近更新 更多