Android Question Array print numbers from array

ronovar

Active Member
Licensed User
Longtime User
I have items in array

B4X:
Dim Items() As Int = Array As Int (1, 2, 3, 4)

I need to get from array this:

1st Call:
4 1 2

2st Call:
1 2 3

3st Call:
2 3 4

4st Call:
3 4 1

5st Call:
4 1 2

So as you can see it start with LAST_ITEM | FIRST_ITEM | SECOND_ITEM

And then incrementing each of three items by one.

This i need for horizontal scroller when i press LEFT Button....When i press RIGHT button it needs to get inverse...

I search forum but could not find function for sort array like i need. I can make it manuall but i like it to calculate thease numbers in example automatically.
 

ronovar

Active Member
Licensed User
Longtime User
I get this code that works as expected but could be short and more faster?

B4X:
'GET - KeyPressed
Sub Activity_KeyPress(KeyCode As Int) As Boolean   
    'SELECT - Remote Code
    Select KeyCode
        Case KeyCodes.KEYCODE_DPAD_LEFT
            'CHECK - Scrolled
            If (Scrolled = 0 Or Scrolled = Items.Length) Then
                Log(Items(Items.Length-1))
                Log(Items(0))
                Log(Items(1))
                Scrolled = 1
            Else
                Log(Items(Scrolled-1))
                Log(Items(Scrolled))
               
                If (Scrolled = Items.Length-1) Then
                    Log(Items(0))
                Else
                    Log(Items(Scrolled+1))
                End If
               
                Scrolled = Scrolled + 1
            End If       
    End Select
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Something like this:
B4X:
Private Items() As Int = Array As Int (1, 2, 3, 4, 5)
Private Scrolled = 2 As Int
Private ItemsToDisplay = 3 As Int

B4X:
Private i, j As Int

Scrolled = Scrolled + 1
If Scrolled = Items.Length Then
    Scrolled = 0
End If

j = Scrolled
For i = 0 To ItemsToDisplay - 1
    Log(Items(j))
    j = j + 1
    If j = Items.Length Then
        j = 0
    End If
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you want to get all permutations of a given string then you can use this library

https://www.b4x.com/android/forum/t...t-all-permutations-of-a-string.61084/#content

B4X:
    Dim l As List = perm.permute("12345")
    For i = 0 To l.Size -1
        Log(l.Get(i))
    Next

LogCat connected to: 9885e6514556383552
--------- beginning of main
--------- beginning of system
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
21435
21453
52143
25143
21543
42153
42135
54213
42513
45213
21354
25134
21345
52134
21534
24153
24135
25413
52413
24513
25341
23415
23451
23541
52341
42351
42531
42315
54231
45231
25431
24351
24315
52431
24531
25314
52314
23154
23514
23145
34251
34215
35421
34521
53421
32514
32145
53214
32154
35214
43521
45321
54321
43215
43251
32541
53241
32451
35241
32415
12435
15243
51243
12453
12543
12345
12354
15234
12534
51234
54123
45123
41235
41253
41523
51423
14253
15423
14523
14235
31452
53142
31542
35142
31425
34125
34152
34512
53412
35412
31254
31524
35124
53124
31245
43125
43512
45312
43152
54312
14325
15432
51432
14532
14352
41325
45132
54132
41352
41532
13254
51324
13245
13524
15324
51342
13452
13542
15342
13425
** Activity (main) Resume **
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Something like this:
B4X:
Private Items() As Int = Array As Int (1, 2, 3, 4, 5)
Private Scrolled = 2 As Int
Private ItemsToDisplay = 3 As Int

B4X:
Private i, j As Int

Scrolled = Scrolled + 1
If Scrolled = Items.Length Then
    Scrolled = 0
End If

j = Scrolled
For i = 0 To ItemsToDisplay - 1
    Log(Items(j))
    j = j + 1
    If j = Items.Length Then
        j = 0
    End If
Next

Thanks this fors as expected is short code and really fast...could you update it to get rewerse way too?

I have LEFT and RIGHT buttons so when i press RIGHT button i get this what you write excellent code...now i need to get hen i press LEFTbutton to go back throught array.

I try to change - where is + but i got error..stil traying to archive in minus way.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Just to save some lines on Klaus answer...

B4X:
Private i, j As Int

Scrolled = (Scrolled + 1) Mod Items.Length

j = Scrolled
For i = 0 To ItemsToDisplay - 1
  Log(Items(j))
  j = (j + 1) Mod Items.Length
Next
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Just to save some lines on Klaus answer...

B4X:
Private i, j As Int

Scrolled = (Scrolled + 1) Mod Items.Length

j = Scrolled
For i = 0 To ItemsToDisplay - 1
  Log(Items(j))
  j = (j + 1) Mod Items.Length
Next

this is shortest and very quck function..thanks....you get
Scrolled + 1 could you write
Scrolled - 1 to get one items back from array? Now it gets one items nex from array
 
Upvote 0
Top