i have the followig list and i want to delete the whole list and leave the last 4 items was inserted in the list based on some condition , is it possible ?
B4X:
dim Type cmdline(command as string)
dim CommandList As List
if CommandList.Size > 50 then
For i = 0 To CommandList.Size - 1
Dim cmd As cmdline
cmd = CommandList.Get(i)
'here i want to delete all items that contain something and leave the last 4 items inserted how can i possibly do that ?
If cmd.command = "something" Then
CommandList.RemoveAt(i)
End If
Next
End if
Not a solution, just a tip. You need to start from the top of the list and work down when you are removing items from the list, otherwise you will not get the results you want.
i have started the loop from top but i am not sure on how to skip the last 4 items in the list while removing
B4X:
if CommandList.Size > 50 then
For i = CommandList.Size -1 To 0 Step -1
Dim cmd As cmdline
cmd = CommandList.Get(i)
If cmd.command = "something" Then
CommandList.RemoveAt(i)
End If
Next
End if
i was asking about the last 4 items that equal to same condition "something" because this list will have multiable items with a different commands name skipping the last 4 items in the total list will remove the last 4 items if they equal the condition "something" but what if the last 4 items are mixed with different names
i think i should calculate each loop with a global integer and count each "something" condition in order to target those conditions only i know that i should set size- if i want to skip this order in list i got misunderstood
Untested but i think it should bring your the in the right direction!?
B4X:
If CommandList.Size > 50 Then
Dim toRemove As List
toRemove.Initialize
For i = CommandList.Size -1 To 0 Step -1
Dim cmd As cmdline
cmd = CommandList.Get(i)
If cmd.command = "something" Then
'CommandList.RemoveAt(i)
toRemove.Add(i)
End If
Next
'
If toRemove.Size>4 Then
For i = toRemove.Size -5 To 0 Step -1
CommandList.RemoveAt(toRemove.Get(i))
Next
End If
End If
'here i want to delete all items that contain something and leave the last 4 items inserted how can i possibly do that ?
Do While CommandList.Size > 4
CommandList.RemoveAt(0)
Loop
If your list items are
1,9,2,9,3,9,4,9,5,9,9,9,9
And the item 9 is what you are looking for, then should the end result be
1,2,3,4,5,9,9,9,9
Or
1,9,2,9,3,9,4,9,5
Or
9,9,9,9 and it is all the 9’s before the 5
Or
9,9,9,9 and it is all the 9’s after the 5