Android Question compare two different size lists

tufanv

Expert
Licensed User
Longtime User
Hello,

I am trying to compare two different size lists
lets say list1 : 24,21,19,17
list2 : 21,17

I want to remove the items from list1 if the item exists in list 2

I am trying below code:

B4X:
Dim item1 As String

For x = 0 To toplamliste.Size - 1
   item1 = toplamliste.Get(x)
  
   For y = 0 To benimliste.Size - 1
      If item1 = benimliste.Get(y) Then
   
         toplamliste.RemoveAt(x)  

      End If
   Next

Next

but this gives me a out of bounds error altough there mustnt be any out of bounds. Can you correct my code what am i doing worng ?

TY
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Any time you are decreasing the length of an array you are iterating through, always start upper bound and go to the lower, e.g.
B4X:
 For x = toplamliste.Size - 1 to 0 Step -1
    For y = benimliste.Size - 1 to 0 Step -1
       ' code here
    Next
Next
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
still getting out of bounds with :
B4X:
For x = toplamliste.Size - 1 To 0 Step -1
    For y = benimliste.Size - 1 To 0 Step -1
       If benimliste.Get(y)=toplamliste.Get(x) Then
           toplamliste.RemoveAt(x)
End If
    Next
Next
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
You should probably have an "Exit" command after your ".RemoveAt" in case the same entry appears more than once in the "benimliste" list. The example below works as expected for me:
B4X:
    Dim poList1 As List 
    Dim poList2 As List
    Dim piOuter As Int
    Dim piInner As Int
    Dim psFruit As String

    poList1.Initialize
    poList1.Add("Apple")
    poList1.Add("Orange")
    poList1.Add("Banana")
    poList1.Add("Grape")
    poList1.Add("Pear")

    poList2.Initialize
    poList2.Add("Kiwi")
    poList2.Add("Apple")
    poList2.Add("Star Fruit")
    poList2.Add("Grape")

    For piOuter = poList1.Size - 1 To 0 Step -1
        psFruit = poList1.Get(piOuter)
        For piInner = poList2.Size - 1 To 0 Step -1
            If psFruit = poList2.Get(piInner) Then
                poList1.RemoveAt(piOuter)
                Exit
            End If
        Next
    Next
    For Each psFruit As String In poList1
        Log("Fruit remaining: " & psFruit)
    Next
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
It looks like size of a list is changing dynamically in the For...Next loop when Removeat called. So For Next loop may not work at such operatings.
This should work.


B4X:
x=0
    Do While x<toplamliste.Size
        y=0
        Do While y<benimliste.Size
            If benimliste.Get(y)=toplamliste.Get(x) Then
                toplamliste.RemoveAt(x)
            End If
            y=y+1
        Loop
        x=x+1
    Loop
    For x=0 To toplamliste.Size-1
        Log(toplamliste.Get(x))
    Next
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
It looks like size of a list is changing dynamically in the For...Next loop when Removeat called. So For Next loop may not work at such operatings.
This should work.


B4X:
x=0
    Do While x<toplamliste.Size
        y=0
        Do While y<benimliste.Size
            If benimliste.Get(y)=toplamliste.Get(x) Then
                toplamliste.RemoveAt(x)
            End If
            y=y+1
        Loop
        x=x+1
    Loop
    For x=0 To toplamliste.Size-1
        Log(toplamliste.Get(x))
    Next
I tried it,

one of my list includes : 21 , 20 , 23
other one : 19 , 20 ,21 , 23

the result with this code gave me a final list of:
19,21 altough it must be 19 only :/
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I will try it now
You should probably have an "Exit" command after your ".RemoveAt" in case the same entry appears more than once in the "benimliste" list. The example below works as expected for me:
B4X:
    Dim poList1 As List
    Dim poList2 As List
    Dim piOuter As Int
    Dim piInner As Int
    Dim psFruit As String

    poList1.Initialize
    poList1.Add("Apple")
    poList1.Add("Orange")
    poList1.Add("Banana")
    poList1.Add("Grape")
    poList1.Add("Pear")

    poList2.Initialize
    poList2.Add("Kiwi")
    poList2.Add("Apple")
    poList2.Add("Star Fruit")
    poList2.Add("Grape")

    For piOuter = poList1.Size - 1 To 0 Step -1
        psFruit = poList1.Get(piOuter)
        For piInner = poList2.Size - 1 To 0 Step -1
            If psFruit = poList2.Get(piInner) Then
                poList1.RemoveAt(piOuter)
                Exit
            End If
        Next
    Next
    For Each psFruit As String In poList1
        Log("Fruit remaining: " & psFruit)
    Next
You should probably have an "Exit" command after your ".RemoveAt" in case the same entry appears more than once in the "benimliste" list. The example below works as expected for me:
B4X:
    Dim poList1 As List
    Dim poList2 As List
    Dim piOuter As Int
    Dim piInner As Int
    Dim psFruit As String

    poList1.Initialize
    poList1.Add("Apple")
    poList1.Add("Orange")
    poList1.Add("Banana")
    poList1.Add("Grape")
    poList1.Add("Pear")

    poList2.Initialize
    poList2.Add("Kiwi")
    poList2.Add("Apple")
    poList2.Add("Star Fruit")
    poList2.Add("Grape")

    For piOuter = poList1.Size - 1 To 0 Step -1
        psFruit = poList1.Get(piOuter)
        For piInner = poList2.Size - 1 To 0 Step -1
            If psFruit = poList2.Get(piInner) Then
                poList1.RemoveAt(piOuter)
                Exit
            End If
        Next
    Next
    For Each psFruit As String In poList1
        Log("Fruit remaining: " & psFruit)
    Next


yes ! this works fine ! Thank you very much !!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
it works....

B4X:
  Dim toplamliste As List
    toplamliste.Initialize
    toplamliste.Add(19)
    toplamliste.Add(20)
    toplamliste.Add(21)
    toplamliste.Add(23)
   
    Dim benimliste As List
    benimliste.Initialize
    benimliste.Add(20)
    benimliste.Add(21)
    benimliste.Add(23)
    Dim x As Int =0
  Dim y As Int
    Do While x<toplamliste.Size
      y=0
    Do While y<benimliste.Size
        If benimliste.Get(y)=toplamliste.Get(x) Then
          toplamliste.RemoveAt(x)
      End If
      y=y+1
    Loop
    x=x+1
  Loop
  For x=0 To toplamliste.Size-1
    Log(toplamliste.Get(x))
  Next
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
it works....

B4X:
  Dim toplamliste As List
    toplamliste.Initialize
    toplamliste.Add(19)
    toplamliste.Add(20)
    toplamliste.Add(21)
    toplamliste.Add(23)
  
    Dim benimliste As List
    benimliste.Initialize
    benimliste.Add(20)
    benimliste.Add(21)
    benimliste.Add(23)
    Dim x As Int =0
  Dim y As Int
    Do While x<toplamliste.Size
      y=0
    Do While y<benimliste.Size
        If benimliste.Get(y)=toplamliste.Get(x) Then
          toplamliste.RemoveAt(x)
      End If
      y=y+1
    Loop
    x=x+1
  Loop
  For x=0 To toplamliste.Size-1
    Log(toplamliste.Get(x))
  Next

this code still gives me 19 and 21 i dont know why no error with you but .. interesting. maybe the sequence of the lists are different so i can get wrong but you are getting correct
my list1: 21,20,23
my list2 : 19,20,21,23
 
Upvote 0

pappicio

Active Member
Licensed User
Longtime User
B4X:
Dim x As Int =0
Do While x<benimliste.Size
      dim z as int=toplamliste.indexof(benimliste.Get(x))
      If z > -1 Then
          toplamliste.RemoveAt(z)
      End If
      x = x + 1
Loop

or:

B4X:
for x=0 to benimliste.Size -1 
      dim z as int=toplamliste.indexof(benimliste.Get(x))
      If z > -1 Then
          toplamliste.RemoveAt(z)
      End If
Next
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
B4X:
Dim x As Int =0
Do While x<benimliste.Size
      dim z as int=toplamliste.indexof(benimliste.Get(x))
      If z > -1 Then
          toplamliste.RemoveAt(z)
      End If
      x = x + 1
Loop

or:

B4X:
for x=0 to benimliste.Size -1
      dim z as int=toplamliste.indexof(benimliste.Get(x))
      If z > -1 Then
          toplamliste.RemoveAt(z)
      End If
Next
This has a small 'bug'. It will not remove multiple instances. Here's a quite similar coding:
B4X:
Dim list1 As List
   list1.Initialize
   list1.AddAll(Array As String("19","20","21","23"))
   Dim list2 As List
   list2.Initialize
   list2.AddAll(Array As String("21","20","23"))
   For i=0 To list2.Size-1
     Dim tempSearch As String=list2.Get(i)
     Dim tempIndex As Int
     tempIndex=list1.IndexOf(tempSearch)
     Do While tempIndex>-1
       list1.RemoveAt(tempIndex)
       tempIndex=list1.IndexOf(tempSearch)
     Loop
   Next
   For j=0 To list1.Size-1
     Log(list1.Get(j))
   Next
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
this works fine overhere

B4X:
For x=benimliste.Size-1 To 0 Step -1
For y=toplamliste.Size-1 To 0 Step -1
     If toplamliste.Get(y)=benimliste.Get(x) Then toplamliste.RemoveAt(y)
Next
Next
Log(toplamliste)

if you used a map you didn't need the double loop either.
 
Upvote 0

pappicio

Active Member
Licensed User
Longtime User
This has a small 'bug'. It will not remove multiple instances. Here's a quite similar coding:
B4X:
Dim list1 As List
   list1.Initialize
   list1.AddAll(Array As String("19","20","21","23"))
   Dim list2 As List
   list2.Initialize
   list2.AddAll(Array As String("21","20","23"))
   For i=0 To list2.Size-1
     Dim tempSearch As String=list2.Get(i)
     Dim tempIndex As Int
     tempIndex=list1.IndexOf(tempSearch)
     Do While tempIndex>-1
       list1.RemoveAt(tempIndex)
       tempIndex=list1.IndexOf(tempSearch)
     Loop
   Next
   For j=0 To list1.Size-1
     Log(list1.Get(j))
   Next
if you don't want multiple instance:
This has a small 'bug'. It will not remove multiple instances. Here's a quite similar coding:
B4X:
Dim list1 As List
   list1.Initialize
   list1.AddAll(Array As String("19","20","21","23"))
   Dim list2 As List
   list2.Initialize
   list2.AddAll(Array As String("21","20","23"))
   For i=0 To list2.Size-1
     Dim tempSearch As String=list2.Get(i)
     Dim tempIndex As Int
     tempIndex=list1.IndexOf(tempSearch)
     Do While tempIndex>-1
       list1.RemoveAt(tempIndex)
       tempIndex=list1.IndexOf(tempSearch)
     Loop
   Next
   For j=0 To list1.Size-1
     Log(list1.Get(j))
   Next

you can prevent double records in list:
B4X:
if list.indexof(newrecord)=-1 then 
   list.add(newrecord)
end if

or use, like you did, multiple check of indexof, into code I posted.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Thanks for all your help !

I am using and it works fine now :

B4X:
For x=benimliste.Size-1 To 0 Step -1
For y=toplamliste.Size-1 To 0 Step -1
     If toplamliste.Get(y)=benimliste.Get(x) Then toplamliste.RemoveAt(y)
Next
Next
Log(toplamliste)
 
Upvote 1
Top