有没有办法在arraylist中找到我感兴趣的所有元素?
让我们说值是
arr.add("mark")
arr.add("john")
arr.add("eli")
arr.add("mark")
arr.add("peter")
我想找到“johns”的索引。
arr.contains
会返回真值或假值 和
arr.indexof("")
将返回FIRST找到值的索引
答案 0 :(得分:4)
我很容易解决问题:
Function duplSearch(val As String, arrayList As Object, i As Long) As Object
Dim temp As Long
Dim results As Object
Set results = CreateObject("System.Collections.Arraylist")
For i = i + 1 To arrayList.Count
If arrayList.indexof(val, i) = -1 Then
i = arrayList.Count
Else
temp = arrayList.indexof(val, i)
results.Add (temp)
i = temp + 1
End If
Next i
Set duplSearch = results.Clone
End Function
答案 1 :(得分:1)
试试这个,
Dim arr As New ArrayList
arr.Add("mark")
arr.add("john")
arr.add("eli")
arr.add("mark")
arr.Add("peter")
arr.Add("john")
Dim i As Integer
For i = 0 To arr.ToArray.Count - 1
If arr(i) = "john" Then
Console.WriteLine("Index Of John :" & i)
End If
Next
结果:
Index Of John : 1
Index Of John : 5
答案 2 :(得分:0)
当您使用System.Collections.ArrayList
时,您也可以使用.IndexOf
。只是不要忘记将,0
写为第二个参数。因此,这很有效:
Option Explicit
Sub TestMe()
Dim testList As Object
Set testList = CreateObject("System.Collections.ArrayList")
testList.Add "mark"
testList.Add "john"
testList.Add "eli-saveta"
testList.Add "peter the keeper with nothing to do"
Debug.Print testList.IndexOf("john", 0)
End Sub
答案 3 :(得分:-1)
要在arraylist中找到e项,您可以使用indexOf(item, start_index)
Set myAL = CreateObject("System.Collections.ArrayList")
myAL.Add ("the")
myAL.Add ("quick")
myAL.Add ("brown")
myAL.Add ("fox")
myAL.Add ("jumps")
myAL.Add ("over")
myAL.Add ("the")
myAL.Add ("lazy")
myAL.Add ("dog")
myAL.Add ("in")
myAL.Add ("the")
myAL.Add ("barn")
Dim myIndex As Integer
myIndex = myAL.IndexOf("the", 0) 'return 0
myIndex = myAL.IndexOf("the", 1) 'return 6
myIndex = myAL.IndexOf("the", 7) 'return 10