I have a list with 3 items and the following only ever gives me the last item of the three entered.
The Dim's are all correct and irrelevant.
I naturally assume size-1 to be the last item in the list as is usual
The log shows the size correctly. Log below. Help please?
B4X:
Do While myList.size > 0
A=myList.Get(myList.size-1)
log("Last " & A)
myList.RemoveAt(myList.size-1)
log("Size " & myList.size)
Loop
The list
[C=6, IsInitialized=false, R=7
[C=6, IsInitialized=false, R=8
[C=7, IsInitialized=false, R=8
The loop output
myList size 3
last [C=7, IsInitialized=false, R=8
Size 2
last [C=7, IsInitialized=false, R=8
Size 1
last [C=7, IsInitialized=false, R=8
Size 0
The list is printed in the log above. There are three items of the same Type but it can be seen that the C,R values differ. The log (first 3 shown above) was printed as the items were added in an i,j loop...
B4X:
Dim posn as Fred
...
posn.c=i : posn.r=j
myList.Add(posn)
Log(myList.Get(myList.Size-1))
[EDIT] I am trying to extract to a smaller app to test but I cannot see what could be different.
As I said, I suspect the list contains the same instance three times. You don't show the complete code but it looks like you only Dim posn once and use it three times. Notice the subtle difference in the code below.
B4X:
...
Dim posn as Fred
posn.c=i : posn.r=j
myList.Add(posn)
Log(myList.Get(myList.Size-1))
Dim myList As List
Dim A As Fred
myList.Initialize
For i=6 To 9
Dim posn As Fred
posn.Initialize
posn.c=i : posn.r=i+2
myList.Add(posn)
Log(myList.Get(myList.Size-1))
Next
Do While myList.size > 0
A=myList.Get(myList.size-1)
Log("Last " & A)
myList.RemoveAt(myList.size-1)
Log("Size " & myList.size)
Loop
I see exactly what you are saying agraham, however, that surely implies that when I place different data in 'the same' posn, that it changes the data already in the list, in other words the list contains a reference to posn NOT its data.
AH!! of course it does.
Thank you for catching me out in that 'classic' error.
@icefairy333. I have just found your entry while I was typing. You have shown me the same flaw.
Thank you all for your help, I hope I do not fall for it again.
When RemoveAt(Index As Int) removes an item, what happens to the content of that location? Does it get filled with a Null or would the list be shifted upwards?