Understanding Lists adding/removing a value
I'm learning lists and adding/removing. In the below code I create a list , generate a random number - and then want to permanently remove the number from the list:
cardsLST.AddAll(Array As Int(1,2,3,4,5)) 'create the list
s = Rnd(1,6) 'generate a random number 1-5
Msgbox(s,"开发者_如何学Go") 'display the randomly generated number
Msgbox(cardsLST,"") 'display the current list members
cardsLST.RemoveAt(s) 'remove the generated value from the list
Msgbox(strtCardsLST,"") 'display the updated list members
I can't make sense of the numbers... for example... If I generate a 1, a 2 is removed from the list. If I generate a 5, I get an out of bounds exception
List and array indeces are zero based so your list is indexed from 0 to 4.
cardsLST.AddAll(Array As Int(1,2,3,4,5))
is equivalent to the following:
cardsLST.insertAT(0,"1") 'cardsLST.isert(Index as int, Value as object)
cardsLST.insertAT(1,"2")
cardsLST.insertAT(2,"3")
cardsLST.insertAT(3,"4")
cardsLST.insertAT(4,"5")
You can use your random generator to generate index value of the card with
s = Rnd(0,5)
to generate a random number 0-4 so
cardsLST.RemoveAt(3)
would remove card "4"
精彩评论