Android Question How can I write a Sub that will remove items or add items to an array?

Widget

Well-Known Member
Licensed User
Longtime User
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.

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
 

DonManfred

Expert
Licensed User
Longtime User
Create a new list inside the sub
Add the items
and then RETURN the list

to remove
remove items from the list inside the sub and then RETURN the list.
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Create a new list inside the sub
Add the items
and then RETURN the list

to remove
remove items from the list inside the sub and then RETURN the list.

1) You mean return the List to an Array variable in the calling sub? I tried that (see code below) and when I attempted to reference the array length in the calling sub, I got an exception. "java.lang.RuntimeException: Field: length not found in: java.util.ArrayList"

2) When you say "create a new list inside the sub... add the items". Is there a fast way to add an array to a list and vice-versa without using a loop?
Note: List1.Initialize2(MyArray) won't work because that prevents the size of List1 from changing. This is just another catch-22 problem that I've encountered.

B4X:
Public Sub TestArray2(aArray() As Int) As List
    Private i As Int
    Private locList As List
   
    locList.Initialize
    For i=0 To aArray.Length-1
        Private locNum As Int
        locNum = aArray(i)
        locList.Add(locNum+1)
    Next
    Return locList
End Sub


sub Button1_Click
    Private locArray(4) As Int
    locArray = Array As Int(1,2,3,4)

    locArray = TestArray2(locArray)
    Log("locArray.Length=" & locArray.Length)     '<-- java.lang.RuntimeException: Field: length not found in: java.util.ArrayList
end sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
<-- java.lang.RuntimeException: Field: length not found in: java.util.ArrayList
you are using array methods but what you get from TestArray2 is not an array. It returns a LIST. And the lenghts of a list you get with mylist.size
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
you are using array methods but what you get from TestArray2 is not an array. It returns a LIST. And the lenghts of a list you get with mylist.size

Right. But when you said:
to remove items from the list inside the sub and then RETURN the list.

I thought you meant return the List that was defined inside the Sub back to the calling sub that was using an Array. The calling sub only used arrays, not List.
If we had access to List.Array then I could simply use: return locList.Array

In any event I have reconciled to the fact that arrays are pretty much fixed length and can't be resized unless I reinitialize them.
Sp as mc73 said, I need to use List. I only went with Arrays because I thought they would be faster.
 
Upvote 0
Top