Android Question LIST2 = SHUFFELIST(LIST1) also changes LIST1, Why?

FERNANDO SILVEIRA

Active Member
Licensed User
Hello Guys,

I have the following code to shuffle LIST1 into LIST2, but at the end both lists are shuffled. I want LIST1 as it was originally.

What am I doing wrong?

B4X:
    Dim lstWords1, lstWords2 As List
    lstWords1 = Array("AVIÃO", "BOLA", "CASA", "COPO", "DADO", "FACA", "FLOR", "FOGO", "FOTO", "GATO", "LEÃO", "MALA", "MENINA", "MENINO", "QUEIJO", "SUCO", "TREM", "XÍCARA", "CÂMARA")
'    lstWords1 = File.ReadList(File.DirAssets, "tabpalavras.txt")
    Log("LIST1 before shuffle: " & lstWords1)
    lstWords2.Initialize2(ShuffleList(lstWords1))
    Log("LIST2  after shuffle: " & lstWords2)  
    Log("LIST1  after shuffle: " & lstWords1)


Sub ShuffleList(StList As List) As List
    Dim wElement As String
    Dim wIndex As Int
    For i = 0 To StList.size - 1
        wIndex = Rnd(i, StList.size)
        wElement = StList.get(i)
        StList.set(i, StList.get(wIndex))
        StList.set(wIndex, wElement)
    Next
    Return StList
End Sub
Regards,
Fernando
 

OliverA

Expert
Licensed User

klaus

Expert
Licensed User
From the List help:
List.Initialize2
Method
Initializes a list with the given values. This method should be used to convert arrays to lists.
Note that if you pass a list to this method then both objects will share the same list,
and if you pass an array the list will be of a fixed size. Meaning that you cannot later add or remove items.
 

LucaMs

Expert
Licensed User
From the List help:
List.Initialize2
Method
Initializes a list with the given values. This method should be used to convert arrays to lists.
Note that if you pass a list to this method then both objects will share the same list,
and if you pass an array the list will be of a fixed size. Meaning that you cannot later add or remove items.
So, to be quite, use always List.Inizialize ;)
 
Top