B4A Class [B4X] List2

LucaMs

Expert
Licensed User
Description: A simple class to add 3 methods to the native List.


Methods:
  • IndexOf2(Item As Object, StartIndex As Int) As Int
    • Returns the index of the specified item starting the search from StartIndex. Returns -1 if Item was not found.
  • GetAllIndexes(Item As Object) As List
    • Returns a list of indexes of the specified item. An initialized empty list will be returned if no items will be found.
  • Count(Item As Object) As Int
    • Returns the number of occurrences of Item.


Properties:
  • List
    • Sets/Gets the list to work with.



B4A example:
B4X:
Sub Process_Globals
   Private lstNames As List
   Private lst2Names As List2
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
   lstNames.Initialize
   lstNames.Add("Arianna")
   lstNames.Add("Barbara")
   lstNames.Add("Claudia")
   lstNames.Add("Barbara")
   lstNames.Add("Daniela")
 
   lst2Names.Initialize(lstNames)
   Dim IndexOf As Int
   IndexOf = lst2Names.IndexOf2("Barbara", 2) ' starts from Claudia
   Log("Index: " & IndexOf)
 
   Dim lstIndexs As List
   lstIndexs = lst2Names.GetAllIndexes("Barbara")
   For Each Index As Int In lstIndexs
       Log(Index & TAB & lstNames.Get(Index))
   Next
 
   Log("Count: " & lst2Names.Count("Barbara"))
End Sub

a) I'm not sure it will work on B4i - I cannot test it
b) It would be possible to implement a Next method, to search for the next occurrence without having to pass the start index.
c) You could move the 3 methods to an utility code module, changing them just a little (passing the source list to them).
 

Attachments

Top