Array question

Ricky D

Well-Known Member
Licensed User
Longtime User
I have an array of usertypes

B4X:
Type typeAddMenuItem(Id As Int, MenuItem As String)
Dim AddMenuItems() As typeAddMenuItem

How do I

1. Add a new entry to the end of the array?

2. Delete an entry from the array?

3. Set the array to be empty?

regards, Ricky
 

Ricky D

Well-Known Member
Licensed User
Longtime User
I have worked it out.

To add a new entry to the array I create a temp array size 1 bigger than the original array. I copy entries from original to temp then add new entry then finally set the original to the temp.

To update I search for the entry and simply replace when it's found.

To delete I'll use a temp array to be 1 size less than the original. Copy entries up to but not including the 1 to be deleted then copy the entries after the 1 being deleted finally set original to the temp.

Tomorrow Brisbane lunchtime I'll code it and post it here (I'm at work now not in front of my pc)

Regards Ricky
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
1. Add a new entry to the end of the array?
This is not possible without reDIMing your array (and therefore loosing all the previous saved data).

2. Delete an entry from the array?
You could use a For-Next-Loop to shift the entries from (n+1) to n.
But the array would remain at the same size. (And reDIMing...)

3. Set the array to be empty?
Just Dim the array again.

If you really need some kind of flexible array I would suggest using Lists or Maps:
Basic4android - Collections (Core)
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
Pseudo Dynamic Arrays

Another idea:
In VB often I've used something that I call "Pseudo Dynamic Arrays"! :cool:

The huge problem with rediming arrays and copying all the data (to preserve the already saved elements) is: It costs to much time!

So I dimmed my "dynamic" array at the beginning at some convenient size (it doesn't really matter):
B4X:
Dim PersonArray(32) As PersonType
And I introduced a variable to count the already saved elements:
B4X:
Dim PersonArrayCount As Int

When I have to save a new entry I used some code to check whether there is still free space. If not: Create more space by redimming and copying!
And this is the essential trick: Double the available space! :)
(Applying the principle of reverse binary search.)

Example:
B4X:
Sub AddPerson(p As PersonType)

  'Calc index of the new element:
  PersonArrayCount = PersonArrayCount + 1

  'Is there still free space?
  If PersonArrayCount > PersonArray.Length Then 'No!

    'In classic VB I would simply use this line:
    '  ReDim Preserve PersonArray(2 * PersonArrayCount)
    'But "Preserve" does not exists in b4a, so do it manualy...

    'Save the old data:
    Dim PersonCache(PersonArray.Length) As PersonType
    Dim i As Int
    For i = 0 To PersonArray.Length - 1
      PersonCache(i) = PersonArray(i)
    Next'i
      
    'Redim the array and copy the old data back:
    Dim PersonArray(2 * PersonArrayCount) As PersonType
    For i = 0 To PersonCache.Length - 1
      PersonArray(i) = PersonCache(i)
    Next'i

  End If

  'Finaly save the new element:
  PersonArray(PersonArrayCount - 1) = p

End Sub

In this way the average extra cost remains very cheap and therefore the programm will run much faster!
 
Last edited:
Upvote 0
Top