【问题标题】:c# Array.FindAllIndexOf which FindAll IndexOfc# Array.FindAllIndexOf which FindAll IndexOf
【发布时间】:2012-05-13 16:20:59
【问题描述】:

我知道 c# 有 Array.FindAllArray.IndexOf

是否有返回int[]Array.FindAllIndexOf

【问题讨论】:

    标签: c# arrays indexof


    【解决方案1】:
    string[] myarr = new string[] {"s", "f", "s"};
    
    int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray();
    

    这将返回 0, 2

    如果数组中不存在该值,则返回一个 int[0]。

    为它创建一个扩展方法

    public static class EM
    {
        public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T val)
        {
            return values.Select((b,i) => object.Equals(b, val) ? i : -1).Where(i => i != -1).ToArray();
        }
    }
    

    然后这样称呼它

    string[] myarr = new string[] {"s", "f", "s"};
    
    int[] v = myarr.FindAllIndexof("s");
    

    【讨论】:

    • 较短的方式 - Enumerable.Range(0, myarr.Length).Where(i =&gt; myarr[i] == "s").ToArray();
    • @NikhilAgrawal 如何修改它以找到浮点数的所有索引等于(在精度内)指定的 val?
    【解决方案2】:

    你可以这样写:

    string[] someItems = { "cat", "dog", "purple elephant", "unicorn" }; 
    var selectedItems = someItems.Select((item, index) => new{
        ItemName = item,
        Position = index});
    

    var Items = someItems.Select((item, index) => new{
        ItemName = item,
        Position = index}).Where(i => i.ItemName == "purple elephant");
    

    阅读Get the index of a given item using LINQ

    【讨论】:

      【解决方案3】:

      搜索与指定谓词定义的条件匹配的元素,并返回整个 System.Array 中出现的所有从零开始的索引。

      public static int[] FindAllIndex<T>(this T[] array, Predicate<T> match)
      {
          return array.Select((value, index) => match(value) ? index : -1)
                  .Where(index => index != -1).ToArray();
      }
      

      【讨论】:

        【解决方案4】:

        我知道这是一篇旧帖子,但您可以尝试以下方法,

        string[] cars = {"Volvo", "BMW", "Volvo", "Mazda","BMW","BMW"};
        var res = Enumerable.Range(0, cars.Length).Where(i => cars[i] == "BMW").ToList();
        

        以列表形式返回 {1,4,5}

        【讨论】:

          【解决方案5】:

          不,没有。但是你可以自己写extension method

          public static int[] FindAllIndexOf<T>(this T[] a, Predicate<T> match)
          {
             T[] subArray = Array.FindAll<T>(a, match);
             return (from T item in subArray select Array.IndexOf(a, item)).ToArray();
          }
          

          然后,为您的数组调用它。

          【讨论】:

          • 不正确,返回第一次出现例如索引 0, 0
          【解决方案6】:

          您可以循环使用findIndex 给出索引

          string[] arr = { "abc", "asd", "def", "abc", "lmn", "wer" };
          int index = -1;
          do
          {
              index = Array.IndexOf(arr, "abc", index + 1);
              System.Console.WriteLine(index);
          } while (-1 != index);
          

          【讨论】:

            【解决方案7】:

            我使用 Nikhil Agrawal 的回答创建了以下相关方法,这可能有用。

            public static List<int> FindAllIndexOf<T>(List<T> values, List<T> matches)
                {
                    // Initialize list
                    List<int> index = new List<int>();
            
                    // For each value in matches get the index and add to the list with indexes
                    foreach (var match in matches)
                    {
                        // Find matches 
                        index.AddRange(values.Select((b, i) => Equals(b, match) ? i : -1).Where(i => i != -1).ToList());
            
                    }
            
                    return index;
                }
            

            它需要一个包含值的列表和一个包含要匹配的值的列表。它返回带有匹配索引的整数列表。

            【讨论】:

              【解决方案8】:

              您可以通过仅创建 2 个整数变量来解决此问题。给你更多的力量!

              string[] seasons= { "Fall","Spring", "Summer", "Fall", "Fall", "Winter"};
              
              int i = 0;
              
              int IndexOfFallInArray = 0;
              
              int[] IndexesOfFall= new int[seasons.Length];
              
              foreach (var item in seasons)
              {
                  if (item == "Fall")
                      {
                          IndexesOfFall[i] = IndexOfFallInArray;
                          i++;
                      }
                      IndexOfFallInArray++;
              }
              

              【讨论】:

                【解决方案9】:

                我知道问题已经得到解答,这只是另一种方法。请注意,我使用了ArrayList 而不是int[]

                // required using directives
                using System;
                using System.Collections;
                
                String      inputString = "The lazy fox couldn't jump, poor fox!";
                ArrayList   locations   =  new ArrayList();      // array for found indexes
                string[] lineArray = inputString.Split(' ');     // inputString to array of strings separated by spaces
                
                int tempInt = 0;
                foreach (string element in lineArray)
                {
                     if (element == "fox")
                     {
                         locations.Add(tempInt);   // tempInt will be the index of current found index and added to Arraylist for further processing 
                     }
                 tempInt++;
                }
                

                【讨论】:

                  猜你喜欢
                  • 2011-08-18
                  • 1970-01-01
                  • 2018-01-17
                  • 2017-04-03
                  • 2013-10-26
                  • 1970-01-01
                  • 2011-01-16
                  • 1970-01-01
                  • 2018-02-18
                  相关资源
                  最近更新 更多