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 ?
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
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
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
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
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
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
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
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
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
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.
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
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
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)