Android Question Multidimensional array

KPmaster

Member
Licensed User
Longtime User
Hi all,
I would like to implement a multimensional array of bytes in my app, but I have no clue on how to set the size for each indipendent dimension.

Declaring the array like this
B4X:
Dim MyArray(10,10) as Byte
I create an array of 10 rows each of them of 10 items.

But what if I want to have the 1st row of 5 items, the 2nd of 7, the 3rd of 3 and so on?
 

DonManfred

Expert
Licensed User
Longtime User
Maybe this is of help for you?
 
Upvote 0

KPmaster

Member
Licensed User
Longtime User
Thanks for your quick reply DonManfred, but I don't think your code could be useful for me.

Follows a sample code of what I want to do.

B4X:
Sub Globals
  Dim Rows(,) as Byte
End Sub

Sub ReadData(data() as Byte)
  Dim totalrows as Int
  Dim rowitems as Int
  Dim actualrow as Int
  Dim item as Int

  Rows = null

  ' Count the number of rows
  totaltows = 0
  For i = 0 To data.Length - 1
    If data(i) = 64 Then totalrows = totalrows + 1
  Next

  ' HERE I SHOULD SET THE NUMBER OF ROWS
  ' BUT THIS IS NOT CORRECT
  Dim Rows(totalrows,) as Byte

  ' Count the items for each row
  actualrow = 0
  rowitems = 0
  For i = 0 To data.Length - 1
    If data(i) = 64 Then

      ' HERE I SHOULD SET THE NUMBER OF ITEMS OF THE CURRENT ROW
      ' BUT THIS IS NOT CORRECT
      Dim Rows(actualrow, rowitems) as Byte

      rowitems = 0
      actualrow = actualrow + 1
    Else
      rowitems = rowitems + 1
    End If
  Next

  ' Now I can put data into the array
  actualrow = 0
  item = 0
  For i = 0 To data.Length
    If data(i) <> 64 Then
      If Rows(actualrow, item) = 0 Then Rows(actualrow, item) = data(i)
      item = item + 1
    Else
      actualrow = actualrow + 1
      item = 0
    End If
  Next
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In a multidimenional array all dimensions have the same value.
In a two dimensional array you cannot have rows with different column numbers.
You you could instead create a List of Lists where each List in the main List has a different length.
Lists can hold any kind of objects, also Bytes.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi KPmaster,

if I read your code correctly, you have a data array where the vale 64 means "start a new row of items".
If this is the case, on your first parsing of the data array you can compute the number of rows (as you do) and the "max number of items in a row" (i.e. the longest intearval between two "64" codes).
Using these values you can then dim an 2D array knowing that it will hold all of your data (and some wasted space..).

Umberto
 
Upvote 0

KPmaster

Member
Licensed User
Longtime User
Klaus,
I will take a look at your suggest, it could be an interesting way. By the way, in Delphi I can create and manage multidimensional array the way I want

udg,
you right and I've already had this thougth by myself, but I don't want some wasted space.
 
Upvote 0
Top