B4J Question Issues when searching for object in list [SOLVED]

CanguroCode

Active Member
Licensed User
Longtime User
I made a class, I instantiated objects of this class and add them to a list.
The problem is that when I want to do a search for a particular object in the list, the indexOf function not seem to find anything.

What am I doing wrong? Do I have to create my own search method?

Thanks in advance


The class 'persona':
B4X:
'Class module
Sub Class_Globals
    Private fx As JFX
    Private nombre As String
    Private apellido As String  
    Private fechaNacimiento As Long
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(aNombre As String, aApellido As String, aFechaNacimiento As Long)
    nombre=aNombre
    apellido=aApellido
    fechaNacimiento=aFechaNacimiento
End Sub

Public Sub obtenerNombre As String
    Return nombre & " " & apellido
End Sub

Public Sub getEdadActual As Int
  
    Return getEdadHasta(DateTime.Now)

End Sub

Public Sub getEdadHasta(fecha As Long) As Int
Dim diferencia As Long
diferencia= fecha - fechaNacimiento
Return Floor(diferencia/DateTime.TicksPerDay/365)
End Sub


The main code:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim personList As List
  
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Principal") 'Load the layout file.
    MainForm.Show
  
  
  
    personList.Initialize
  
  
      For i=0 To 10
     Dim p As persona
     p.Initialize("NamePerson"&i, "LastName", DateTime.DateParse ("01/28/1960"))
     personList.Add(p)
     Next
  
  
  
    For i=0 To personList.Size-1
     Dim temporal As persona
     temporal= personList.Get(i)
     Log(temporal.obtenerNombre)
    Next
 
   Dim p As persona
   p.Initialize("NamePerson1", "LastName", DateTime.DateParse ("01/28/1960"))


   Log( personList.IndexOf(p))


End Sub
 

Daestrum

Expert
Licensed User
Longtime User
I think you will have to search the list yourself

The reason why I think it doesn't work.

When you create an object , in this case, the persona class object, Java assigns a unique reference to this object.

You then add it to the list , but I believe Java only stores a reference to the object in the list, not the actual object.

When you create your search persona, in the last part of your code, you are creating yet another object, that object will never be in the list unless you add it, so indexOf will always return not found in list.

You can check this by storing a copy of an object, then using that in the search, indexOf will return the correct item number.
B4X:
    Dim l As List
    l.Initialize
    Dim test As smallclass
'==== smallclass def in class module ====
'Dim n As Int
'Dim s As String

'Public Sub Initialize (nn As Int,ss As String)
'n = nn
's = ss
'End Sub
'===================
    For p = 0 To 10
    Dim rec As smallclass
    rec.Initialize(p,"rec"&p)
    If p = 2 Then test = rec '  save a copy of this object
    l.Add(rec)
    Next
    Log(l)
    Dim f As smallclass
    f.Initialize(2,"rec2")
    Log(f)
    Log(test)
    Log(l.IndexOf(f)) ' fails - object not in list
    Log(l.IndexOf(test)) ' as test is a copy of rec2 it is found in list
    Log(f = test) ' will return false as they are not the same object

In my example above you can add two routines to the class to enable you to get the index

B4X:
Public Sub compareTo(r As smallclass) As Boolean
    If n = r.n AND s = r.s Then 
        Return True
    Else
        Return False
    End If
End Sub
Public Sub indexInList(l As List) As Int
    Dim reply As Int = -1
    For i = 0 To l.Size -1
        Dim tt As smallclass
        tt = l.Get(i)
        If n = tt.n AND s = tt.s AND reply = -1 Then reply = i
      Next
Return reply
End Sub

then you can use
B4X:
    Log(f.compareTo(test))
    Log("in list pos : " & f.indexInList(l))

The first line will return true, even though they are different objects.
The second will return the position in the list of the item (zero based) or -1 if not found in list.

Hope this helps.
 
Last edited:
Upvote 0

CanguroCode

Active Member
Licensed User
Longtime User
Man! thanks! you take my problem seriously and you solved it. I will read again your answer/post with calm and i will test the proposal.

Again thanks a lot!
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Had a think and rewrite of the routines to make them more efficient
B4X:
Public Sub compareTo(r As smallclass) As Boolean
    If n = r.n AND s = r.s Then Return True
    Return False
End Sub
Public Sub indexInList(l As List) As Int
    For Each tt As smallclass  In l
        If compareTo(tt) Then Return l.IndexOf(tt)
    Next
    Return -1
End Sub
 
Upvote 0

CanguroCode

Active Member
Licensed User
Longtime User
Thank you very much for your time invested in this post. With the last thing you added, this is more complete. Changing the subject: solved.

Thanks, again
 
Upvote 0
Top