iOS Question Merge two String Arrays

aeric

Expert
Licensed User
Longtime User
Referring to the code snippet for B4A here:

B4X:
Public Sub Merge (array1() As String, array2() As String) As String()
    Dim BC As ByteConverter
    Dim array3(array1.Length + array2.Length) As String
    BC.ArrayCopy(array1, 0, array3, 0, array1.Length)
    BC.ArrayCopy(array2, 0, array3, array1.Length, array2.Length)
    Return array3
End Sub

In B4i, I am getting errors at line #4 and #5 when calling BC.ArrayCopy:
B4X:
Cannot cast type: {Type=String,Rank=1, RemoteObject=True} to: {Type=Byte,Rank=1, RemoteObject=True}

For B4i, I can't use the above code snippet but to copy the items one by one?
 

aminoacid

Active Member
Licensed User
Longtime User
In B4i, I am getting errors at line #4 and #5 when calling BC.ArrayCopy:
B4X:
Cannot cast type: {Type=String,Rank=1, RemoteObject=True} to: {Type=Byte,Rank=1, RemoteObject=True}

Why don't you use "object" instead of "string"
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Since B4I byteconverter's arrayCopy seems to be limited to bytes, this can be a nice replacement (only tested in debug mode)
B4X:
    Dim array1() As String = Array As String("1","2","3","4","5")
    Dim array2() As String = Array As String("6","7","8")
    Dim array3() As String
    array3.As(List).AddAll(array1.As(List))
    array3.As(List).AddAll(array2.As(List))
    For k=0 To array3.Length-1
        Log(array3(k))      '<-- 1 2 3 4 5 6 7 8
    Next
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Since B4I byteconverter's arrayCopy seems to be limited to bytes
Yes.
Have you tested with non-integer items in the list/arrays?

Recently I am updating a B4X library (MiniORMUtils) which required array of String as parameter in Execute method.
I will test it when I have time.
Thanks.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Public Sub Merge (array1() As String, array2() As String) As String()
    Dim res(array1.Length + array2.Length) As String
    For i = 0 To array1.Length - 1
        res(i) = array1(i)
    Next
    For i = 0 To array2.Length - 1
        res(i + array1.Length) = array2(i)
    Next
    Return res
End Sub
Thanks Erel.
This is what I am using right now.

B4X:
#If B4A or B4i
Public Sub AddParameters (Params() As String)
    'DBParameters = Merge(DBParameters, Params)
    If Params.Length = 0 Then Return
    If DBParameters.Length > 0 Then
        Dim NewArray(DBParameters.Length + Params.Length) As String
        For i = 0 To DBParameters.Length - 1
            NewArray(i) = DBParameters(i)
        Next
        For i = 0 To Params.Length - 1
            NewArray(DBParameters.Length + i) = Params(i)
        Next
        DBParameters = NewArray
    Else
        DBParameters = Params
    End If
End Sub
#Else
Public Sub AddParameters (Params As List)
    DBParameters.AddAll(Params)
End Sub
#End If
 
Upvote 0

emexes

Expert
Licensed User
what I am using right now

Is there a reason that DBParameters has to be an Array? Can you make it a List? Much simpler to update, eg your AddParameters Sub becomes an inline:

DBParameters.AddAll(Params)

and I'm pretty sure that even literal extras like:

DBParameters.AddAsll( Array As String("extrapar1", "extrapar2", "extrapar3=45") )

would work.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Is there a reason that DBParameters has to be an Array? Can you make it a List? Much simpler to update, eg your AddParameters Sub becomes an inline:

DBParameters.AddAll(Params)

and I'm pretty sure that even literal extras like:

DBParameters.AddAsll( Array As String("extrapar1", "extrapar2", "extrapar3=45") )

would work.
Thanks for the suggestion.
I have tried to use a List but it seems B4i doesn't like it.
If I remember correctly, it is DBUtils.ExecuteMap(DBSQL, DBStatement, DBParameters) in B4A or B4i that expect a String array in 3rd argument. In B4J, I am using a List which is more convenient.
I welcome anyone to fork my repo and improve MiniORMutils library.
 
Upvote 0
Top