Android Question List question[SOLVED]

I was wondering about iterations in lists. I read that "for each" is better than "for i", but I can't seem to grasp it completely. I a, specifically looking for a way to go through a list and get an item that is the first item on the list that is greater than a given value.. For example...

example:
Dim x as int = 12
Dim mylist As List
mylist.Initialize
mylist.AddAll(Array As Int(5, 10, 15, 20, 25))

if x > mylist.get(i) then
"stop iterating and return that first value"
Or maybe it would be better to get the ceil that's less than x?
I am hoping this is explained well enough. I want it to ultimately subtract 10 from 12 and give the remainder maybe using mod
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
For Each is not exactly better than the regular For loop. Use the one that is easier for you.

B4X:
Sub FindFirstGreaterValue (List As List, Value As Int) As Int
    For Each v As Int In List
        If v > Value Then Return v
    Next
    Return -1
End Sub

B4X:
Dim i As Int = FindFirstGreaterValue(Array As Int(5, 10, 15, 20, 25), 12)
Log(i)
 
Upvote 0
i'm going to write a separate question about this in a different thread, but i didn't know if you could maybe explain a little further...If I wanted it to do the same thing if x = i how would that look?
Would i different subs if I wanted the both to happen within my app? or could they be combined into the same sub? I guess a better question would be where can I find the documentation that explain what exactly each part is in your solution so I could implement as I see fit?
 
Upvote 0
Top