List As Array

leporter37204

Member
Licensed User
Longtime User
I understood that lists are pretty much like dynamic arrays. However, I created a list, initialized it, and added a single string to it. Later when I try to assign that list to another list variable I get a java ClassCastException saying java.lang.String cannot be cast to java.util.list. This doesn't make sense to me. When I hover the cursor over the variables in question in debug mode they're displayed as ArrayList[oneelement]. Why is that considered a string? Thanx.
 

leporter37204

Member
Licensed User
Longtime User
The Code

I can't display all the code. Hopefully, this excerpt will suffice

I have a class for all my DB stuff that contains:

Dim RMed As List
Dim RKey As List
Dim RSpinners As List

A different activity reads the record(s) and assigns the values to the lists like:

Dim x As Int
Dim mRec As Map

'These guys have been initialized the same place as the other DB stuff
MyMDB.RMed.Clear
MyMDB.RKey.Clear

For x = 0 To MyMDB.MRecords.Size - 1
Dim hMDesc(1) As String
Dim hMKey(1) As Int

mRec = MyMDB.MRecords.Get(x)
hMDesc(0) = (mRec.GetValueAt(MyMDB.M.MDescription))
MyMDB.RMed.AddAll(hMDesc)
hMKey(0) = (mRec.GetValueAt(MyMDB.M.MKey))
MyMDB.RKey.AddAll(hMKey)
Next

MyMDB.RSpinners.AddAll(MyMDB.RMed)

Later when I try to assign to a list variable:

Dim holdList As List

holdList.Initialize
holdList = MyMDB.RSpinners.Get(spinnerPos)

The loop used to assign to the lists directly. I thought maybe if I used an array, assigned the value to the array, and then assigned the array to the list it might work. No dice. When I do the assignment to the holdList variable it pukes. Thanx for your help.
 
Last edited:
Upvote 0

leporter37204

Member
Licensed User
Longtime User
Additional Testing

I did some additional testing. Here's what I discovered. If I do this:

Dim x as list
Dim y as list

x.addall(array as string("1","2"))
y.addall(x)

The x and y variables display like this in the debugger:

x (ArrayList) [1,2]
y (ArrayList) [1,2]

But if I do this:

Dim x as list
Dim y as list
Dim z as list

x.addall(array as string("1","2"))
y.addall(array as string("1","2"))
z.add(x)
z.add(y)

Then the debugger displays x and y as before but z as:

z (ArrayList) [[1,2],[1,2]]

So in the first instance it appears that y is an array of strings. In the second instance z is an array of list objects. That seems like inconsistent behavior. In my case, since I don't know how many records I have in the database, I need a way to always have an array of objects. Is there some other way to do that?
 
Last edited:
Upvote 0

leporter37204

Member
Licensed User
Longtime User
Sorry, didn't know about the code tags. The problem, at least to me, is in the first instance, even though I added a list, the list I added it to treated it as a string. So I ended up with a list that contained an "array" of strings rather than an "array" of list objects. The second construct gave me an "array" of list objects. At least that's the way it appeared. So when I tried to assign a member of the array to another list object I got an assignment error. So it would seem I have to try and determine what kind of objects the list holds. I could do a try/catch and assume if it's not holding list objects it must be strings but I don't understand why the behavior isn't consistent. If I add a list object, no matter what that list object contains, why isn't the target list storing it as a list object rather than, seemingly, mutating it to a string? Thanx.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…