Android Question Check if array exists?

ilan

Expert
Licensed User
Longtime User
hi

i have a list with 10 (array as double(x,y)) in it

i would like to go in a loop and take out duplicated arrays

is it possible??

with a string it is simple

B4X:
If l.IndexOf("123") > -1 Then
    'string exists already
End If

but if i try this with an array of doubles it doesnot work


EDIT: i think i will just use a string and the split it to an array of doubles...
 

ilan

Expert
Licensed User
Longtime User
I don't understand the question. What do you exactly want? Check if array contains a number?

no

i have lots of vectors in my list
every vector has a x point and a y point

the vector look like this

B4X:
dim dbl() as double = array as double(100,200)

now i add that vector to a list and would like to check if the same item already exists before i add it to the list

with a string its simple

i can ask with indexof or go with a loop and check if list1.get(i) = string

but i cannot do this with a list that every item is an array

i could read the array and check each value like this:

B4X:
Dim found As Boolean

For Each dbl As Double In list1
    If newdbl(0) = dbl(0) And newdbl(1) = dbl(1) Then
        found = True
        Exit 
    End If
Next

If found Then list1.add(newdbl)

but with a more bigger array or custom types it would be to much to check:

B4X:
Type part(left As Float, top As Float, touched As Boolean, id As Int, originx As Float, originy As Float, color As Int)

it would be nice if i just could say:

B4X:
Dim found As Boolean

For Each p As part In list1
    If newpart = p Then
        found = True
        Exit
    End If
Next

If found Then list1.add(newpart)

but thats wont work...
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
no

i have lots of vectors in my list
every vector has a x point and a y point

the vector look like this

B4X:
dim dbl() as double = array as double(100,200)

now i add that vector to a list and would like to check if the same item already exists before i add it to the list

with a string its simple

i can ask with indexof or go with a loop and check if list1.get(i) = string

but i cannot do this with a list that every item is an array

i could read the array and check each value like this:

B4X:
Dim found As Boolean

For Each dbl As Double In list1
    If newdbl(0) = dbl(0) And newdbl(1) = dbl(1) Then
        found = True
        Exit
    End If
Next

If found Then list1.add(newdbl)

but with a more bigger array or custom types it would be to much to check:

B4X:
Type part(left As Float, top As Float, touched As Boolean, id As Int, originx As Float, originy As Float, color As Int)

it would be nice if i just could say:

B4X:
Dim found As Boolean

For Each p As part In list1
    If newpart = p Then
        found = True
        Exit
    End If
Next

If found Then list1.add(newpart)

but thats wont work...

Sorry if I have not understood the question.

I tried this way and I think it worked:

B4X:
Type part(left As Float, top As Float, touched As Boolean, id As Int, originx As Float, originy As Float, color As Int)

Dim Item1, Item2,Item3 As part
  
Item1.originx=100 : Item1.originy=200 : Item1.touched=True
Item2.originx=75 : Item2.originy=50 : Item2.touched=True
Item3.originx=125 : Item3.originy=225 : Item3.touched=False
  
Dim list1 As List :  list1.Initialize
list1.AddAll(Array As Object(Item1,Item2,Item3))
  
  
Dim ItemToCheck As part  ' Equal ITEM2
ItemToCheck.originx=75 : ItemToCheck.originy=50 : ItemToCheck.touched=True
  
For i=0 To list1.Size-1
     Log(CheckFound(ItemToCheck,list1.Get(i)))
Next

Sub CheckFound(o1 As Object, o2 As Object) As Boolean
  
    Dim check1,check2 As part
    check1=o1 : check2=o2
  
    Return(check1.color=check2.color And check1.id=check2.id _
    And check1.left=check2.left And check1.originx=check2.originx _
    And check1.top=check2.top And check1.touched=check2.touched)
              
End Sub
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Ilan, you could try anyway to use Types instead of arrays, although I fear that the command IndexOf should not work as it should not work if you use objects (classes).

Damn, as always I'm curious and now I am forced to give it a try! :D

I do so many tests that if I had the Erel's memory between short time I would get a Nobel Prize :p
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Ilan, you could try anyway to use Types instead of arrays, although I fear that the command IndexOf should not work as it should not work if you use objects (classes).

Damn, as always I'm curious and now I am forced to give it a try! :D

I do so many tests that if I had the Erel's memory between short time I would get a Nobel Prize :p

Done and as expected it does not work.


Well, only 8 minutes wasted :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok here is a more dynamic solution:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Type part(left As Float, top As Float, touched As Boolean, id As Int, originx As Float, originy As Float, color As Int)
    Type vector(x As Double, y As Double)
    Dim MultiList As List
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
    MultiList.Initialize
  
    'START ADD ALL OBJECTS TO MULTI LIST
    For i = 0 To 9 'add 10 type vectors to the list
        Dim v As vector
        v.x = i*50
        v.y = i*50
        MultiList.Add(v)
    Next

    For j = 0 To 9 'add 10 arrays as double to the list
        Dim dbl() As Double = Array As Double(j*50,j*50)
        MultiList.Add(dbl)
    Next
  
    For k = 0 To 9
        Dim p As part
        p.id = k
        p.color = fx.Colors.To32Bit(fx.Colors.Red)
        p.left = k*50
        p.top = k*50
        p.originx = k*50
        p.originy = k*50
        p.touched = True
        MultiList.Add(p)
    Next
    'END ADD ALL OBJECTS TO MULTI LIST
  
    'CHECK IF ITEM EXITS
  
    Dim searchv As vector
    searchv.x = 3*50
    searchv.y = 3*50
    Log("pos: " & DynamicIndexOf(MultiList,searchv)) 'should return pos 3
  
    Dim searchdbl() As Double = Array As Double(100,100)
    Log("pos: " & DynamicIndexOf(MultiList,searchdbl)) 'should return pos 12  
  
    Dim searchp As part
    searchp.id = 5
    searchp.color = fx.Colors.To32Bit(fx.Colors.Red)
    searchp.left = 5*50
    searchp.top = 5*50
    searchp.originx = 5*50
    searchp.originy = 5*50
    searchp.touched = True
    Log("pos: " & DynamicIndexOf(MultiList,searchp)) 'should return pos 25  
  
    Dim searchdbl2() As Double = Array As Double(125,125)
    Log("pos: " & DynamicIndexOf(MultiList,searchdbl2)) 'should return pos -1  
End Sub

Sub DynamicIndexOf(MyList As List, Obj As Object) As Int
    For i = 0 To MyList.Size - 1
        Dim listobj As Object  = MyList.Get(i)
        If GetType(listobj) <> GetType(Obj) Then Continue
       
        If listobj Is part And Obj Is part Then 
            Dim p = MyList.Get(i), pobj = Obj As part 
            If p.id = pobj.id And p.color = pobj.color And p.left = pobj.left And p.top = pobj.top And p.originx = pobj.originx And _
                p.originy = pobj.originy And p.touched = pobj.touched Then Return i
        else if listobj Is vector And Obj Is vector Then   
            Dim v = MyList.Get(i), vobj = Obj As vector 
            If v.x = vobj.x And v.y = vobj.y Then Return i
        else if GetType(listobj) = "[D" Then 'Array as double
            Dim d() = MyList.Get(i), dobj() = Obj As Double 
            If d(0) = dobj(0) And d(1) = dobj(1) Then Return i               
        End If
    Next
    Return -1
End Sub

t5.png



Just because of curiosity, why when i set 2 array of doubles that are identical and ask like this

B4X:
Log(dbl1 = dbl2)

why i do not get TRUE, they are the same doubles? are arrays handled differently?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @Erel,
I don't know if it's doable or not so, on an eventual positive short response by you, I'll post a wish in the proper Forum section otherwise we can consider the idea dismissed here.
What about a MemoryBlock object? I mean, an object that reflects a given block of memory. It should have the ability to map any other object and return it as an array of bytes; it should sport a comparator function that gets TRUE when two MemoryBlocks refers to memory regions made of identical values; eventually a function that tells us which is the first occurrence of no correspondence between two blocks. Well, guess you got the idea. Is it doable?
 
Upvote 0
Top