温馨提示×

vb怎么在数组中查找指定元素

vb
小亿
169
2023-10-31 18:50:11
栏目: 编程语言

在VB中,可以使用Array类的IndexOf方法来查找数组中的指定元素。具体用法如下:

Dim arr As Integer() = {1, 2, 3, 4, 5}
Dim target As Integer = 3

Dim index As Integer = Array.IndexOf(arr, target)

If index >= 0 Then
    Console.WriteLine("元素 {0} 在数组中的索引位置为 {1}", target, index)
Else
    Console.WriteLine("元素 {0} 不在数组中", target)
End If

上述代码中,我们创建了一个整型数组arr,并指定了一个目标元素target为3。然后使用Array.IndexOf方法来查找目标元素在数组中的索引位置。如果该元素存在于数组中,则打印出对应的索引值;如果不存在,则打印出相应的提示信息。

0