M MathiasM Active Member Licensed User Jan 14, 2021 #1 Hello I try to add a value to a list, but I get a java.lang.UnsupportedOperationException when I try to do this. Why is this not working? Code: Copy Sub AppStart (Args() As String) TestList("Third", Array As String("First", "Second")) End Sub Sub TestList(NewValue As String, PreviousList As List) Dim NewList As List NewList.Initialize2(PreviousList) NewList.Add(NewValue) End Sub Thanks!
Hello I try to add a value to a list, but I get a java.lang.UnsupportedOperationException when I try to do this. Why is this not working? Code: Copy Sub AppStart (Args() As String) TestList("Third", Array As String("First", "Second")) End Sub Sub TestList(NewValue As String, PreviousList As List) Dim NewList As List NewList.Initialize2(PreviousList) NewList.Add(NewValue) End Sub Thanks!
stevel05 Expert Licensed User Longtime User Jan 14, 2021 #2 Because Initialize2 creates a fixed size list. The same as if you do: B4X: Copy Dim l As List = Array("Test","Test") l.Add("Test") You need to do: B4X: Copy Dim NewList As List NewList.Initialize NewList.AddAll(PreviousList) NewList.Add(NewValue) Upvote 0
Because Initialize2 creates a fixed size list. The same as if you do: B4X: Copy Dim l As List = Array("Test","Test") l.Add("Test") You need to do: B4X: Copy Dim NewList As List NewList.Initialize NewList.AddAll(PreviousList) NewList.Add(NewValue)
M MathiasM Active Member Licensed User Jan 14, 2021 #3 Indeed! Thanks a lot. I was unaware of fixed sized lists. It is mentioned in the documentation. Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Jan 15, 2021 #4 Tip: never use List.Initialize2. As @stevel05 wrote you can convert an array to a read-only list directly. Upvote 0
Tip: never use List.Initialize2. As @stevel05 wrote you can convert an array to a read-only list directly.