Bug? List.Initialize2 and Array [SOLVED, I've should've read the documentation]

alwaysbusy

Expert
Licensed User
Longtime User
Maybe not really a bug, but more a not so obvious limitation of using a list initialize2 with an array

As i'm working on my B4JS Transpiler engine, I'm making some test methods to see if my conversion works. I'm trying out all kind of weird combinations to test the Transpiler, and I've got some UnsupportedOperationExceptions when a B4J list is initialized with Initialize2 and Array. There is no indication when I write the code in B4J this is wrong and at first sight I think it should work, put they come up when I run it. Happens with typical real List methods (things you can not do with an array) like
AddAllAt() and RemoveAt()

Note those are B4J errors, not B4JS Transpiler errors. This is the working version + with comments if I use List2Arr.Initialize2() instead of List2Arr.Initialize() + manual adds. That is when the errors come up.

B4X:
Sub B4JSTestList()
   Log("------- Start B4JSTestList")
   Dim List1 As List
   List1.Initialize
   List1.Add("test1")
   List1.Add("test2")
   
   Dim strDV As String = List1.Get(0)
   Log("test1 <=> " & strDV)
   strDV = List1.Get(1)
   Log("test1 <=> " & strDV)
   
   Dim List2Arr As List
   'List2Arr.Initialize2(Array("test1", "test2", "test3", "test4", "test5", "test6")) ' UnsupportedOperationException in B4JSList.Initialize2 (see further)
   ' this works
   List2Arr.Initialize
   List2Arr.Add("test1")
   List2Arr.Add("test2")
   List2Arr.Add("test3")
   List2Arr.Add("test4")
   List2Arr.Add("test5")
   List2Arr.Add("test6")
   
   Dim List3List As List
   List3List.Initialize2(List2Arr)
   
   Dim List4 As List
   ' List4.Initialize2(Array As String("test-2", "test-1", "test0")) ' UnsupportedOperationException in AddAllAt(2, List3List) (see further). I suspect because List3List is using initialize2 + An Array
   ' this works
   List4.Initialize
   List4.Add("test-2")
   List4.Add("test-1")
   List4.Add("test0")
   List4.AddAllAt(2, List3List) ' <- exception if using List2Arr.Initialize2()
   Log("test-1 <=> " & List4.Get(1))
   Log("test1 <=> " & List4.Get(2))
   Log("test2 <=> " & List4.Get(3))
   Log("test0 <=> " & List4.Get(8))
   
   ' B4JSList is declared as a global variable   
   B4JSList.Initialize2(List2Arr)
   Dim bool1 As Boolean = B4JSList.IsInitialized
   Log("true, but in B4JS will always be true <=> " & bool1)
   B4JSList.RemoveAt(1) ' UnsupportedOperationException if using List2Arr.Initialize2()
   Log("test3 <=> " & B4JSList.Get(1))
   
   Dim int1 As Int = B4JSList.IndexOf("test3")
   B4JSList.Set(int1, "newtest1") ' I suspect the same error, but it never reaches this far
   Log("newtest1 <=> " & B4JSList.Get(1))
   
   B4JSList.InsertAt(3, "newtest2") ' I suspect the same error, but it never reaches this far
   Log("newtest2 <=> " & B4JSList.Get(3))
   
   B4JSList.Sort(True) ' I suspect the same error, but it never reaches this far
   Log("newtest1 <=> " & B4JSList.Get(0))
   B4JSList.Sort(False) ' I suspect the same error, but it never reaches this far
   Log("test6 <=> " & B4JSList.Get(0))
   B4JSList.SortCaseInsensitive(bool1)   ' I suspect the same error, but it never reaches this far
   Log("newtest1 <=> " & B4JSList.Get(0))
   
   B4JSList.Clear
   Log("0 <=> " & B4JSList.Size)
   
   ' not supported in B4JS
   Dim list5 As List
   list5.Initialize
   Dim i As Int
   For i = 0 To 5
     ' is declared as global: Type testtype(field1 As Int, field2 As Int)
     Dim tmptype As testtype
     tmptype.Initialize
     tmptype.field1 = i
     tmptype.field2 = i+1
     list5.Add(tmptype)
   Next
   list5.SortType("field1", True)
   Log("SortType is not supported in B4JS")
   list5.SortTypeCaseInsensitive("field1", True)
   Log("SortTypeCaseInsensitive is not supported in B4JS")
End Sub

The errors when I use Initialize2 instead of Initialize + adding manually:

B4X:
------- Start B4JSTestList
test1 <=> test1
test1 <=> test2
Error occurred on line: 215 (B4JSTestPage) <-- AddAllAt()
java.lang.UnsupportedOperationException
   at java.util.AbstractList.add(AbstractList.java:148)
   at java.util.AbstractList.addAll(AbstractList.java:260)
   at anywheresoftware.b4a.objects.collections.List.AddAllAt(List.java:86)
   at xplaydemo.ab.com.b4jstestpage._b4jstestlist(b4jstestpage.java:538)
   at xplaydemo.ab.com.b4jstestpage._buildpage(b4jstestpage.java:768)
   at xplaydemo.ab.com.b4jstestpage._initialize(b4jstestpage.java:73)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:226)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
   at xplaydemo.ab.com.main.main(main.java:29)

Alwaysbusy
 
Top