2012-05-04 11 views
18

मुझे पता है सी # Array.FindAll और Array.IndexOf है।सी # Array.FindAllIndexOf जो FindAll IndexOf

क्या Array.FindAllIndexOf है जो int[] देता है?

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"); 

पढ़ें:

उत्तर

21
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"); 
+2

छोटा रास्ता - 'संख्यात्मक। श्रेणी (0, myarr.Length)। जहां (i => myarr [i] ==" s ")। ToArray();' – diesel

+0

@ निखिल अग्रवाल यह सभी को खोजने के लिए कैसे संशोधित किया जा सकता है एक निर्दिष्ट वैल के बराबर (परिशुद्धता के भीतर) फ्लोटिंग पॉइंट संख्याओं की अनुक्रमणिका? – gwizardry

1

आप 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); 
2

नहीं है, वहाँ नहीं है दे सकते हैं कहते हैं। लेकिन आप अपना खुद का 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(); 
} 

और फिर, अपने सरणी के लिए, इसे कॉल करें।

3

निर्दिष्ट तत्व द्वारा परिभाषित शर्तों से मेल खाने वाले तत्व के लिए खोजें, और पूरे सिस्टम के भीतर घटना के सभी शून्य-आधारित इंडेक्स को लौटाता है। ऐरे।

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(); 
} 
-2

मुझे पता है कि प्रश्न का उत्तर पहले ही दिया गया है, यह ऐसा करने का एक और तरीका है। ध्यान दें कि मैं 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++; 
} 
0

की।

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; 
    } 

जो मानों के साथ एक सूची लेता है और मिलान के साथ मूल्यों के साथ एक सूची लेता है। यह मैचों की अनुक्रमणिका के साथ पूर्णांक की एक सूची देता है।