B4J Question List Size Query

NewB4JUser

Member
Licensed User
Hi I used the follwing to find the amount of entries in my list:

For Each id As Int In Vlist
Log(id)
Next

(ArrayList) [9, 68, , , 61, , 17, , 9, , , 79, , 34, 2, , 34, 38, 55, 12, , 51, , 75, 36, , ]

But returned this:

How many items in my Vlist 0

I guessing its due to the commas, but how do i look beyoned these.

Please if anyone can help?
 

emexes

Expert
Licensed User
Longtime User
Try this, for a better idea of what's in your list:

B4X:
Log("There are " & Vlist.Size & " items in VList")

For I = 0 To VList.Size - 1
     Log(I & Tab & VList.Get(I))
Next
 
Upvote 0

NewB4JUser

Member
Licensed User
Thanks so much for this. Works perfectly, However tried to use this on another list and it displays the 0 values too, how would I be specific to say >0 please

Example list:

0,,,,,,0,2,,,,,3,5,,,,,9,,,,0,0,0,0,0,,,,,0,,,,0,,,,0,0,8,,,,,,7,,,,,, ..............................etc
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
it displays the 0 values too, how would I be specific to say >0 please

Without knowing exactly what is in your list, it's a bit hard to say. Is the list Strings, including "" and "0"? Or is it mostly Ints but sometimes nuls?

Anyway, maybe something like this:

B4X:
For I = 0 To VList.Size - 1
    Dim Obj As Object = VList.Get(I))    'could be Int, could be String, could be Null...
    If Obj Is Int Then
        Log(I & Tab & Obj)
    End If
Next

or maybe a snipped out of this:

B4X:
Dim L As List
L.Initialize

For I = 1 To 50
    If Rnd(0, 2) = 0 Then
        L.Add("")
    else if Rnd(0, 2) = 0 Then
        L.Add(0)
    Else
        L.Add(Rnd(1, 10))
    End If
Next

Log(L.Size)
Log(L)

Dim sb As StringBuilder
sb.Initialize
Dim Count As Int = 0
For I = 0 To L.Size - 1
    Dim Obj As Object = L.Get(I)    'could be Int, could be String, could be Null...
    If Obj Is Int Then
        If Obj > 0 Then
            If Count > 0 Then
                 sb.Append(",")    'add separator if needed
            End If
            Count = Count + 1
            sb.Append(Obj)
        End If
    End If
Next
Log(sb.ToString)

Dim Count As Int = 0
For I = 0 To L.Size - 1
    Dim Obj As Object = L.Get(I)
    If Obj Is Int Then
        If Obj > 0 Then
            Count = Count + 1
            Log(I & TAB & Count & TAB & Obj)
        End If
    End If
Next
Log output:
Waiting for debugger to connect...
Program started.
50
(ArrayList) [, , 8, 8, , , 0, , 0, , 6, 0, , 0, , 0, , , 5, 0, 1, , , , 9, 0, , , , , , 2, 0, , , 0, 9, 7, 0, , 0, , 8, 1, , 9, 1, 8, , 0]
8,8,6,5,1,9,2,9,7,8,1,9,1,8
2    1    8
3    2    8
10    3    6
18    4    5
20    5    1
24    6    9
31    7    2
36    8    9
37    9    7
42    10    8
43    11    1
45    12    9
46    13    1
47    14    8
Program terminated (StartMessageLoop was not called).
 
Upvote 0
Top