Length of array problem

sterlingy

Active Member
Licensed User
Longtime User
Folks,

I declared an array, allowing a maximum of 50 elements:

Dim myArray(50) As String

But I don't always use the entire 50 elements. Sometimes, I may only load it up with 15 elements or some other random number of elements.

Is there a way to determine the number of elements in the array?

:sign0085: -Sterling
 

admac231

Active Member
Licensed User
Longtime User
SOmething like this would work:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim myArray(50) As String
Log(countElements(myArray))
End Sub

Sub countElements(arr() As String)
   Dim n As Int
   n = 0
   For i = 0 To arr.Length - 1
      If arr(i) <> "" Then
         n = n + 1
      End If
   Next
   Return n
End Sub

Alternatively you could use a map instead of an array which would allow you to use myMap.Size as well as only being the size that you need.
 
Upvote 0

caccas

Member
Licensed User
Longtime User
List



Hi admac231,

You should use a List.

bye
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You can simply place the size of the array in position 0, thus you could have something like this:
B4X:
Dim arr(51)
then
B4X:
arr(0)=sizeOfTheArray
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
No problem. I wrote it in the forum so the syntax probably wouldn't be correct.

To echo what everyone else has said though, you should use a list. Something I completely overlooked when replying.
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
To echo what everyone else has said though, you should use a list. Something I completely overlooked when replying.

I can't find documentation on LIST. If you can point me in the right direction, I'll check it out.

Cheers,

Sterling
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
Have a look here.

Essentially, instead of using
B4X:
Dim myArray(50) As String
myArray(0) = "x"

Log(myArray(0))
you would use
B4X:
Dim myList As List
myList.Initialise
myList.Add("x")

Log(myList.Get(0))

This then lets you use methods like Size
B4X:
Log(myList.Size)
and other handy things like sorting. It also keeps the list at the size that you need so if you only have 15 elements you aren't wasting memory by allocating space for 50.
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Admac231,

I want to try and convert my program from using an Array to a List.

I'm a bit confused as to how best to do this.

Currently, I have defined a TYPE and then declared the Array:

B4X:
Type typeQuestion( _
           Question As String, correctAnswer As Int, _
           Answer1 As String, Answer2 As String, Answer3 As String, Answer4 As String)
           
   Dim Questions(50) As typeQuestion

Then I populate the elements with data:

B4X:
      Questions(2).Question = "Enrique plays tennis every Saturday. It takes him 30 minutes, each way from his house to the court and he plays for 1 hour. How much time does he spend between traveling and playing?"
   Questions(2).Answer1 = "2.5 Hours?"
   Questions(2).Answer2 = "2 Hours?"
   Questions(2).Answer3 = "1.5 Hours?"
   Questions(2).Answer4 = "3 hours?"
   Questions(2).correctAnswer = 2

FYI, once I get things running properly, I will be populating the array/list with data from a mySQL server.

Later in the code, I modify my views with the data from the Questions Array.

I'm not sure how to add the Question TYPE to the list and still be able to access each element.

I hope this is making sense.

:sign0085: Sterling
 
Upvote 0

admac231

Active Member
Licensed User
Longtime User
Makes perfect sense

What to do is add each question separately rather than in an array. So, create a list:

B4X:
Dim questions As List
   questions.Initialize
Then create your question:
B4X:
Dim Question As typeQuestion
   Question.Question = "Enrique plays tennis every Saturday. It takes him 30 minutes, each way from his house to the court and he plays for 1 hour. How much time does he spend between traveling and playing?"
   Question.Answer1 = "2.5 Hours?"
   Question.Answer2 = "2 Hours?"
    Question.Answer3 = "1.5 Hours?"
    Question.Answer4 = "3 hours?"
    Question.correctAnswer = 2
Then add this to the list:
B4X:
questions.Add(Question)
Then when you want to access the question use the List.Get method to put the element back into your Question object:
B4X:
Question = Questions.Get(0)
Then you can use it as normal. So:

Question.Question = Enrique plays tennis every Saturday. It takes him 30 minutes, each way from his house to the court and he plays for 1 hour. How much time does he spend between traveling and playing?

Question.Answer1 = 2.5 hours
e.t.c e.t.c

Here's my little 'test app' if you learn better that way:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
Type typeQuestion( _
              Question As String, correctAnswer As Int, _
              Answer1 As String, Answer2 As String, Answer3 As String, Answer4 As String)
End Sub

Sub Activity_Create(FirstTime As Boolean)

              
   Dim Question As typeQuestion
   Question.Question = "Enrique plays tennis every Saturday. It takes him 30 minutes, each way from his house to the court and he plays for 1 hour. How much time does he spend between traveling and playing?"
   Question.Answer1 = "2.5 Hours?"
   Question.Answer2 = "2 Hours?"
    Question.Answer3 = "1.5 Hours?"
    Question.Answer4 = "3 hours?"
    Question.correctAnswer = 2
   Dim Questions As List
   Questions.Initialize
   Questions.Add(Question)
   Question = Questions.Get(0)
   Log(Question.Question)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…