📜  C#中的Array.LastIndexOf方法|套装– 1

📅  最后修改于: 2021-05-29 22:24:07             🧑  作者: Mango

Array.LastIndexOf方法用于查找一维数组中的最后一个匹配元素。它从数组的最后一个元素开始搜索。它返回包含指定值的元素的索引。此方法的重载列表中有6种方法,如下所示:

  • LastIndexOf(Array,Object)
  • LastIndexOf(Array,Object,Int32)
  • LastIndexOf(Array,Object,Int32,Int32)
  • LastIndexOf (T [],T)
  • LastIndexOf (T [],T,Int32)
  • LastIndexOf (T [],T,Int32,Int32)

在这里,我们仅讨论前三种方法。

LastIndexOf(Array,Object)

此方法搜索指定的对象,并返回整个一维Array中最后一次出现的索引。

返回值:如果找到值的索引,则此方法返回该在整个数组中最后一次出现的索引。如果未找到,则数组的下限减去1。

例外情况:

  • ArgumentNullException:如果数组arr为null。
  • RankException:如果数组arr是多维的。

范例1:

// C# program to demonstrate the 
// Array.LastIndexOf(Array, 
// Object) method
using System;
  
class GFG {
  
    static void Main()
    {
        int[] arr = {1, 2, 6, 8, 6, 2};
  
        // search for "6" from end of the array
        // returns index 4
        int indx1 = Array.LastIndexOf(arr, 6);
          
        Console.WriteLine("First Index of 6 search"+
                     " from end found at " + indx1);
  
        // search for "2" from end of the array
        // returns index 5
        int indx2 = Array.LastIndexOf(arr, 2);
         
        // when the object is found then 
        // the search will stop searching
        Console.WriteLine("First Index of 2 search "+
                       "from end found at " + indx2);       
    }
}
输出:
First Index of 6 search from end found at 4
First Index of 2 search from end found at 5

范例2:

// C# program to demonstrate the 
// Array.LastIndexOf(Array, 
// Object) method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a array of type Array
        Array arr = Array.CreateInstance(typeof(String), 8);
  
        // 0, 1, 2, 3, 4, 5, 6, 7 are 
        // the indexes of the elements
        arr.SetValue("java", 0);
        arr.SetValue("C#", 1);
        arr.SetValue("C++", 2);
        arr.SetValue("C", 3);
        arr.SetValue("Python", 4);
        arr.SetValue("C#", 5);
        arr.SetValue("C++", 6);
        arr.SetValue("Ruby", 7);
          
        String s1 = "C#";
        int index1 = Array.LastIndexOf(arr, s1);
  
        Console.WriteLine("First Index of C# search"+
                     " from end found at " + index1);
  
        String s2 = "C++";
        int index2 = Array.LastIndexOf(arr, s2);
  
        Console.WriteLine("First Index of C++ search"+
                     " from end found at " + index2);
    }
}
输出:
First Index of C# search from end found at 5
First Index of C++ search from end found at 6

LastIndexOf(Array,Object,Int32)

此方法搜索指定的对象,并返回从第一个元素到指定索引的一维Array元素范围内最后一次出现的索引。

返回值:如果找到了值的索引,则此方法返回数组“ arr”中从第一个元素开始的元素范围内该值最后一次出现的索引。如果未找到,则数组的下限减去1。

例外情况:

  • ArgumentNullException:如果数组arr为null。
  • RankException:如果数组arr是多维的。
  • ArgumentOutOfRangeException:如果“开始”索引超出数组的有效索引范围。

范例1:

// C# program to demonstrate the 
// Array.LastIndexOf(Array, Object,
// Int32) method
using System;
  
class GFG {
  
    static void Main()
    {
        int[] arr = {1, 2, 6, 8, 6, 2};
  
        // search for "6"
        // search start from index 3
        // so returns index 2
        int indx1 = Array.LastIndexOf(arr, 6, 3);
          
        Console.WriteLine("First Index of 6 search "+
                   "from index 3 found at " + indx1);
  
         // search for "2"
         // search start from index 2
        // so returns index 1
        int indx2 = Array.LastIndexOf(arr, 2, 2);
         
        Console.WriteLine("First Index of 2 search "+
                   "from index 2 found at " + indx2);
    }
}
输出:
First Index of 6 search from index 3 found at 2
First Index of 2 search from index 2 found at 1

范例2:

// C# program to demonstrate the 
// Array.LastIndexOf(Array, Object, 
// Int32) method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a array of type Array
        Array arr = Array.CreateInstance(typeof(String), 8);
  
        // 0, 1, 2, 3, 4, 5, 6, 7 are the
        // indexes of the elements
        arr.SetValue("java", 0);
        arr.SetValue("C#", 1);
        arr.SetValue("C++", 2);
        arr.SetValue("C", 3);
        arr.SetValue("Python", 4);
        arr.SetValue("C#", 5);
        arr.SetValue("C++", 6);
        arr.SetValue("Ruby", 7);
          
        String s1 = "C#";
  
        // the search start from index 4
        // so returns index 1
        int index1 = Array.LastIndexOf(arr, s1, 4);
         
        Console.WriteLine("First Index of C# search "+
                   "from index 4 found at " + index1);
  
        String s2 = "C++";
  
        // the search start from index 4
        // so returns index 2
        int index2 = Array.LastIndexOf(arr, s2, 3);
          
        Console.WriteLine("First Index of C++ search"+
                  " from index 3 found at " + index2);
    }
}
输出:
First Index of C# search from index 4 found at 1
First Index of C++ search from index 3 found at 2

LastIndexOf(Array,Object,Int32,Int32)

此方法搜索指定的对象,并返回包含指定数量的元素并在指定索引处结束的一维数组中元素范围内最后一次出现的索引。

返回值:如果找到值的索引,则此方法返回该值最后一次出现的索引,该在数组“ arr”中的元素范围内,该数组包含“ count”中指定的元素数,并在“ start”处结束”指数。如果未找到,则数组的下限减去1。

例外情况:

  • ArgumentNullException:如果数组arr为null。
  • RankException:如果数组arr是多维的。
  • ArgumentOutOfRangeException:如果“开始”索引超出数组的有效索引范围,或者“计数”小于零,或者“开始”索引和计数未在数组中指定有效部分。

范例1:

// C# program to demonstrate the 
// Array.LastIndexOf(Array, 
// Object, Int32, Int32) method
using System;
  
class GFG {
  
    // Main Method
    static void Main()
    {
        int[] arr = {1, 5, 6, 2, 6, 8, 2};
  
        // search for "6"
        // search start from index 5 and
        // searches 2 indexes backward from index 5
        // at first index 4 is comes where the value is 6
        // so returns index 4
        int indx1 = Array.LastIndexOf(arr, 6, 5, 2);
          
        Console.WriteLine("First Index of 6 search from"+
                           " index 5 found at " + indx1);
  
        // search for "2"
        // search start from index 4 and
        // searches 2 indexes backward from index 4
        // at first index 3 is comes where the value is 2
        // so returns index 3
        int indx2 = Array.LastIndexOf(arr, 2, 4, 2);
          
        Console.WriteLine("First Index of 2 search from"+
                           " index 4 found at " + indx2);
    }
}
输出:
First Index of 6 search from index 5 found at 4
First Index of 2 search from index 4 found at 3

范例2:

// C# program to demonstrate the 
// Array.LastIndexOf(Array, Object,
// Int32, Int32) method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating a array of type Array
        Array arr = Array.CreateInstance(typeof(String), 8);
  
        // 0, 1, 2, 3, 4, 5, 6, 7 are the 
        // indexes of the elements
        arr.SetValue("java", 0);
        arr.SetValue("C#", 1);
        arr.SetValue("C++", 2);
        arr.SetValue("C", 3);
        arr.SetValue("C++", 4);
        arr.SetValue("C#", 5);
        arr.SetValue("Python", 6);
        arr.SetValue("Ruby", 7);
          
        String s1 = "C#";
  
        // search for "C#"
        // search start from index 6 and
        // searches 3 indexes backward from index 6
        // at first index 5 is comes where the value is "C#"
        // so returns index 5
        int index1 = Array.LastIndexOf(arr, s1, 6, 3);
          
        Console.WriteLine("First Index of C# search from"+
                           " index 6 found at " + index1);
  
        String s2 = "C++";
  
        // search for "C++"
        // search start from index 5 and
        // searches 3 indexes backward from index 5
        // at first index 4 is comes where the value is "C++"
        // so returns index 4
        int index2 = Array.LastIndexOf(arr, s2, 5, 3);
         
        Console.WriteLine("First Index of C++ search from"+
                            " index 5 found at " + index2);
    }
}
输出:
First Index of C# search from index 6 found at 5
First Index of C++ search from index 5 found at 4

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.array.lastindexof?view=netframework-4.7.2