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

Discussion in 'Android Questions' started by FERNANDO SILVEIRA, Apr 22, 2018.

  1. FERNANDO SILVEIRA

    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?

    Code:
    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 ListAs 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
     
  2. Mahares

    Mahares Well Known Member Licensed User

    This is how I would do it:
    Code:
    Log("LIST1  before shuffle: " &lstWords1)
        lstWords2.Initialize
        lstWords2.AddAll(lstWords1)
        lstWords2=ShuffleList(lstWords2)
        
    Log("LIST2  after shuffle: " &lstWords2)
        
    Log("LIST1  after shuffle: " & lstWords1)
     
    FERNANDO SILVEIRA likes this.
  3. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Mahares, it worked your way. Thanx

    I just don't understand the concept behind my way and yours. Maybe @Erel could provide me more details.
     
  4. Mahares

    Mahares Well Known Member Licensed User

    When you use shufflelist(lstwords1) in your code, you are re-shulling lstwords1 again which you do not want to do.
     
    FERNANDO SILVEIRA likes this.
  5. OliverA

    OliverA Well-Known Member Licensed User

    FERNANDO SILVEIRA likes this.
  6. klaus

    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.
     
    FERNANDO SILVEIRA and LucaMs like this.
  7. LucaMs

    LucaMs Expert Licensed User

    So, to be quite, use always List.Inizialize ;)
     
    FERNANDO SILVEIRA likes this.
  8. FERNANDO SILVEIRA

    FERNANDO SILVEIRA Active Member Licensed User

    Thank you all.
    I was not familiar with the concept primitive/non-primitive types.

    Cheers! ;)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice