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

 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User

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

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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…