Android Question How to convert a list to a two-dimensional array?

I write a function of converting a list to a two-dimensional array, but it run wrong.
It seems the problem is the 15 line code .
B4X: Copy
Private Sub toArray (Region As Int)
    Private Array1() As String
    Private Array2(5 , ) As String
    Private filename As String
    filename = Region & ".csv"
  
    Dim parser As CSVParser
    parser.Initialize
    Dim data As List = parser.Parse(File.ReadString(File.DirAssets, filename), ",", True)
    For index = 1 To data.Size
        Array1(index)=data.Get(index)
    Next
  
    For i = 1 To data.Size
        For j = 1 To Array1.size
            Array2(i , j) = Array1(j)
        Next
    Next
    Return Array2
  
End Sub
 
Last edited:
Upvote 0
I modify the code below. But it still run wrong.
Can you give me a help?

B4X: Copy
Private Arraypara(,) As String
Arraypara = toArray(1)

Private Sub toArray (Regionnum As Int)
    Private Array1() As String
    Private Array2(,) As String
    Private filename As String
    filename = "Region" & Regionnum & ".csv"
    
    Dim parser As CSVParser
    parser.Initialize
    Dim data As List = parser.Parse(File.ReadString(File.DirAssets, filename), ",", True)
    For index = 1 To data.Size
        Array1(index)=data.Get(index)
    Next
    
    For i = 0 To data.Size-1
        For j = 0 To Array1.Length-1
            Array2(i , j) = Array1(j)
        Next
    Next
    
    Return Array2
    
End Sub
 
Upvote 0
Open perplexity.ai and write your question:
b4a code: converting a list to a two-dimensional array
Then you will see the magic :)
 
Upvote 0
This may be suitable. If the matrix is numeric and there is missing data.

B4X: Copy
    Dim simulData As String = $"c1,c2,c3,c4${CRLF}1,2,3,4${CRLF}10,20,30${CRLF}100,200,,400${CRLF}1000,2000,3000,4000"$
    Dim parser As CSVParser
    parser.Initialize
    Dim lines As List = parser.Parse(simulData, ",", True)
    Dim firstLine() As String = lines.Get(0)
    Dim ncols As Int = firstLine.length
    Dim nrows As Int = lines.size
    Dim matrix(nrows, ncols) As Float
    Log(nrows & TAB & ncols)
    
    For i = 0 To nrows - 1
        Dim w() As String = lines.Get(i)
        For j = 0 To ncols - 1
            If IsNumber(w(j)) Then matrix(i, j) = w(j) Else matrix(i, j) = 0/0            'NaN
        Next
    Next

    For i = 0 To nrows - 1
        Dim sb As StringBuilder
        sb.Initialize
        For j = 0 To ncols - 1
            sb.Append(NumberFormat2(matrix(i, j), 5, 0, 0, False)).Append(TAB)
        Next
        Log(sb.ToString)
    Next

result:
4 4
00001 00002 00003 00004
00010 00020 00030 NaN
00100 00200 NaN 00400
01000 02000 03000 04000
 
Upvote 0

William Lancee​

This may be suitable. If the matrix is numeric and there is missing data.


For i = 0 To nrows - 1
Dim sb As StringBuilder
sb.Initialize
For j = 0 To ncols - 1
sb.Append(NumberFormat2(matrix(i, j), 5, 0, 0, False)).Append(TAB)
Next
Log(sb.ToString)
Next
If the two-dimensional arrays are not in the same Sub , what is the code?
 
Upvote 0
If the two-dimensional arrays are not in the same Sub , what is the code?
Better use te CODE tags to show:

B4X: Copy
For i = 0 To nrows - 1
Dim sb As StringBuilder
sb.Initialize
For j = 0 To ncols - 1
sb.Append(NumberFormat2(matrix(i, j), 5, 0, 0, False)).Append(TAB)
Next
Log(sb.ToString)
Next
 
Upvote 0
@daiweisc

The following uses the same terminology as your code.
Note that there are many approaches to this, especially how to pass back multiple results like the dimensions of Array2

If you just return Array2 by using "Private Sub toArray (Regionnum As Int) As String(,)"
It will be hard (not impossible) to get the dimensions of the 2-dimensional array.

[Don't forget to scroll down to see the full code! If you don't, you'll miss the point]

B4X: Copy
Private Sub Button1_Click
    Dim result As Map = toArray(1)       'calls the generalized Sub
    Dim dataMatrix(,) As String = result.Get("matrix")
    Dim nrows As Int = result.Get("nrows")
    Dim ncols As Int = result.Get("ncols")
  
    For i = 0 To nrows - 1
        Dim sb As StringBuilder
        sb.Initialize
        For j = 0 To ncols - 1
            sb.Append(dataMatrix(i,j)).Append(TAB)
        Next
        Log(sb.ToString)
    Next
End Sub

Private Sub toArray (Regionnum As Int) As Map
    Dim parser As CSVParser
    parser.Initialize

'These 2 lines are replaced for testing purposes
'    filename = "Region" & Regionnum & ".csv"
'    Dim data As List = parser.Parse(File.ReadString(File.DirAssets, filename), ",", True)

    Dim simulData As String = $"c1,c2,c3,c4${CRLF}A,B,C,D${CRLF}D,E,F,G${CRLF}H,I,J,K${CRLF}W,X,Y,Z"$  'my test data
    Dim data As List = parser.Parse(simulData, ",", True)

    Dim firstLine() As String = data.Get(0)
    Dim ncols As Int = firstLine.length
    Dim nrows As Int = data.size
    Dim Array2(nrows, ncols) As String
  
    For i = 0 To data.Size-1       'or nrows - 1
        Dim Array1() As String = data.Get(i)
        For j = 0 To Array1.Length-1    'or ncols - 1
            Array2(i , j) = Array1(j)
        Next
    Next
    Return CreateMap("matrix": Array2, "nrows": nrows, "ncols": ncols)
End Sub
 
Upvote 0
Back
Top