Android Question dynamic array?

vecino

Well-Known Member
Licensed User
Hi, is it possible to do something like this?
B4X:
Sub aaaaa
  Dim aNumbers() As int
End Sub

Sub bbbbb
  Dim iAmount as int = DBconex.ExecQuerySingleResult("select count(id) from tbSome")
   
  aNumbers.SetLength( iAmount )        ' <--- Can you do something similar?
   
End Sub
 

JTmartins

Active Member
Licensed User
Do you mean SetLenght as in Delphi ?

In that case you better use a List type, as it grows dinamicaly when you add itens to it and you do not need to care with array dimensions.

However, if your intention is just to use an array of fixed dimension, you can do something like

B4X:
Dim iAmount as int=DBconex.ExecQuerySingleResult("select count(id) from tbSome")

  Dim aNumbers( iAmount )
but ensure that iAmount is a valid value...You probably do not want to dimension an array with 0 elements :)
 

vecino

Well-Known Member
Licensed User
Thanks for your answer.

JTmartins said:
Do you mean SetLenght as in Delphi ?
Yes, exactly :)

JTmartins said:
In that case you better use a List type, as it grows dinamicaly when you add itens to it and you do not need to care with array dimensions.
However, if your intention is just to use an array of fixed dimension, you can do something like
but ensure that iAmount is a valid value...You probably do not want to dimension an array with 0 elements :)
The problem is that "aNumbers" is a public variable in another activity.
 

LucaMs

Expert
Licensed User
Same.

MyModule.aNumbers(iAmount)

;)

aNumbers should be declared as Public!



[P.S. no, it does not work.
You could use a List o a Map (better solution) or:

MyModule:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Public aNumbers() As Int
End Sub

Public Sub SetNumbersCount(N As Int)
    Dim aNumbers(N) As Int
End Sub

Elsewhere (activity):
B4X:
MyModule.SetNumbersCount(9)
MyModule.aNumbers(1) = 234
Log(MyModule.aNumbers(1))
 
Last edited:

vecino

Well-Known Member
Licensed User
MyModule:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Public aNumbers() As Int
End Sub

Public Sub SetNumbersCount(N As Int)
    Dim aNumbers(N) As Int
End Sub
But in that code "aNumbers" are different variables, one public and one private.
 

LucaMs

Expert
Licensed User
But in that code "aNumbers" are different variables, one public and one private.
Try, it works.

But you should use a List or a Map (I prefer Maps; you can consider them as a kind of matrix, two-dimensional arrays, with the advantage of being able to identify each element by a name (or an object!), in addition to what I write below).

Using a List instead of an array of Int, you can:
B4X:
  Public lstNumbers As List : lstNumbers.Initialize

   ' "resize" the List automatically
   lstNumbers.Add(123)

   ' Insert a List (or array) inside the List
   Dim InsertAt As Int = 2
   lstNumbers.AddAllAt(InsertAt, Array As Int(1,2,3))

   ' Search for a value in the List (value can be an object)
   Dim IndexOf5 As Int = lstNumbers.IndexOf(5)

   ' Remove an element
   lstNumbers.RemoveAt(3)

   ' Set a value for an element
   lstNumbers.Set(2, 567)

   ' Sort the List
   lstNumbers.Sort(True)
Can you do all this with arrays?
 
Last edited:
Top