當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


VB.NET Array LastIndexOf()用法及代碼示例

LastIndexOf() 方法用於搜索數組中的項目並返回該項目最後一次出現的索引。

用法:

Function LastIndexOf (ByVal arr() as object, Byval item as integer) as Integer

參數:

  • arr:它是指定的整數數組。
  • Item:要在數組中搜索的項目。

返回值:它返回數組中最後一次出現的項目的索引。

程序/源代碼:

下麵給出了演示 Array 類的 LastIndexOf() 方法的源代碼。給定的程序已成功編譯並執行。

'VB.NET program to demonstrate the LastIndexOf() 
'method of Array class.

Imports System

Module Module1
    Sub Main()
        Dim arr() As Integer = {10, 20, 30, 20, 30}
        Dim index As Integer

        'Search the specified item in the array and return 
        'the index of the last occurrence of an item.
        index = Array.LastIndexOf(arr, 20)

        Console.WriteLine("Index of last occurrence of item {0} is:{1}", 20, index)
        Console.WriteLine()
    End Sub
End Module

輸出:

Index of last occurrence of item 20 is:3
Press any key to continue . . .

說明:

在上麵的程序中,我們創建了一個包含 Main() 函數的模塊 Module1。 Main() 函數是程序的入口點。

在 Main() 函數中,我們創建了一個數組 arr1。然後我們使用 Array 類的 LastIndexOf() 方法獲取數組中最後一次出現的項目的索引。之後,我們在控製台屏幕上打印了項目的索引。





相關用法


注:本文由純淨天空篩選整理自 VB.Net program to demonstrate the LastIndexOf() method of Array class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。