B4A Class [B4X] List2

Discussion in 'Additional libraries, classes and official updates' started by LucaMs, Aug 8, 2018.

Tags:
  1. LucaMs

    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:
    Code:
    Sub Process_Globals
       
    Private lstNames As List
       
    Private lst2Names As List2
    End Sub

    Code:
    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).
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice