This seems like a simple task, to write a sub that takes an array parameter and remove or add items to the array. But nothing I have tried will work.
The obvious method would be to pass the array to a Sub that has a parameter List. B4A automatically converts the array to a list and now I can manipulate the List in TestList to add or remove entries. What the B4A manual didn't tell me was the List cannot perform Add/InsertAt/RemoveAt operations without throwing a "Unsupported Operation Exception". This is odd because the values in the aList can be modified and the changes are returned to the calling sub.
I suppose I could use an Array parameter instead of a List parameter, but then I would have to find some way of efficiently assigning the array elements to the list and then back again.
The following code won't work because the "locList.Initialize2(aArray)" apparently just assigns the array address to locList because the locList.Add(1234) still throws the same exception.
Of course I can write a couple of Subs that will take 2 parameters, an array and a list and go through all of the items in the array and assign it to the list, and vice versa. But this is grossly inefficient because the subs could be called a few thousand times.
Is there any efficient way to add/remove items from an array? Or should I throw arrays out of the window and switch to using a List instead?
I prefer using an array because 99% of the time the array size won't have to be changed and it should be much faster to access elements of an array compared to a List.
TIA
B4X:
public Sub TestList(aList As List)
aList.Insertat(0, aList.Size) '<- Unsupported Operation Exception
End Sub
public sub Test
Private locArray(4) As Int
locArray = Array As Int(1,2,3,4)
TestList(locArray) 'Pass the array into a List
end sub
The obvious method would be to pass the array to a Sub that has a parameter List. B4A automatically converts the array to a list and now I can manipulate the List in TestList to add or remove entries. What the B4A manual didn't tell me was the List cannot perform Add/InsertAt/RemoveAt operations without throwing a "Unsupported Operation Exception". This is odd because the values in the aList can be modified and the changes are returned to the calling sub.
I suppose I could use an Array parameter instead of a List parameter, but then I would have to find some way of efficiently assigning the array elements to the list and then back again.
The following code won't work because the "locList.Initialize2(aArray)" apparently just assigns the array address to locList because the locList.Add(1234) still throws the same exception.
B4X:
Public Sub TestArray(aArray() As Int)
Private locList As List
locList.Initialize2(aArray)
locList.Add(1234) '<- Unsupported Operation Exception
aArray = locList 'How do I assign the locList back to aArray?
End Sub
Of course I can write a couple of Subs that will take 2 parameters, an array and a list and go through all of the items in the array and assign it to the list, and vice versa. But this is grossly inefficient because the subs could be called a few thousand times.
Is there any efficient way to add/remove items from an array? Or should I throw arrays out of the window and switch to using a List instead?
I prefer using an array because 99% of the time the array size won't have to be changed and it should be much faster to access elements of an array compared to a List.
TIA