List size - removeat problem

enonod

Well-Known Member
Licensed User
Longtime User
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
 

melamoud

Active Member
Licensed User
Longtime User
You should cast a to the right type and print it
I think it will be ok.

Is a a string ? An object of some type...
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thanks for reply.
A is Dim'd as the correct type. It is a User Type i.e.

Type Fred(C as Byte, R as Byte)

Dim A as Fred

Surely that is enough? Also note that it does recover an item, the last and so surely everything knows what everything is.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Is it part of a larger app? Have you tried making a small test app that still fails? If you have can you post it.

via Tapatalk
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
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.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
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))
 
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
B4X:
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
works ok!
logcat:
B4X:
** Activity (main) Create, isFirst = true **

[C=6, IsInitialized=true, R=8
]
[C=7, IsInitialized=true, R=9
]
[C=8, IsInitialized=true, R=10
]
[C=9, IsInitialized=true, R=11
]
Last [C=9, IsInitialized=true, R=11
]
Size 3
Last [C=8, IsInitialized=true, R=10
]
Size 2
Last [C=7, IsInitialized=true, R=9
]
Size 1
Last [C=6, IsInitialized=true, R=8
]
Size 0
** Activity (main) Resume **
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

RichardBernard

Member
Licensed User
Longtime User
RemoveAt(Index As Int)

Hi guys,

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?


Thanks,
R
 
Upvote 0
Top