简体   繁体   中英

multi dimensional jagged array initialization

I am working on NFC mifare card reader and am trying to create a byte array like this, but i get an error

int FIX_SECTOR_COUNT = 16;

const int numOfSector = 16;
const int numOfBlockInSector = 4;
byte[][][] buffer = new byte[FIX_SECTOR_COUNT][numOfSector][numOfBlockInSector];

Error CS0178 Invalid rank specifier: expected ',' or ']

so i did this

byte[][][] buffer = new byte[][][] { };

but i need help instantiating it.

i need the byte array to do something like this:

try
{
    taskTag.Connect();

    for (int s = 0; s < numOfSector; s++)
    {
        if (taskTag.AuthenticateSectorWithKeyA(s, MifareClassic.KeyDefault.ToArray()))
        {
            for (int b = 0; b < numOfBlockInSector; b++)
            {
                int blockIndex = (s * numOfBlockInSector) + b;
                buffer[s][b] = taskTag.ReadBlock(blockIndex);
            }
        }
    }

    success = true;
}
catch (Java.IO.IOException e)
{
    e.PrintStackTrace();
}

if (success)
{
    string stringBlock = "";
    for (int i = 0; i < numOfSector; i++)
    {
        stringBlock += i + " :\n";
        for (int j = 0; j < numOfBlockInSector; j++)
        {
            for (int k = 0; k < MifareClassic.BlockSize; k++)
            {
                stringBlock += string.Format("%02X", buffer[i][j][k] & 0xff) + " ";
            }
            stringBlock += "\n";
        }
        stringBlock += "\n";
    }
}

To initialize a 2D jagged array you need one loop.

int[][] data = new int[rows][];
for(int i=0; i<rows; i++)
{
    int[] row = new int[cols];
    // assign values to row
    data[i] = row;
}

Now extend this to 3D jagged arrays

byte[][] buffer = new byte[FIX_SECTOR_COUNT][][];
for(int i=0; i<FIX_SECTOR_COUNT; i++)
{
    byte[][] sector = new int[numOfSector][];
    for(int j=0; j<numOfSector; j++)
    {
        byte[] block = new byte[numOfBlockInSector];
        // fill values for block
        sector[j] = block;
    }
    buffer[i] = sector;
}

Of course, it is best to create an function to initialize jagged arrays of various dimensions for code re-use.

For example this is code I have in my library for 2D jagged arrays:

    /// <summary>
    /// Create an empty jagged array of rows×cols
    /// </summary>
    public static T[][] CreateJaggedArray<T>(int rows, int cols)
    {
        var matrix=new T[rows][];
        for(int i=0; i<rows; i++)
        {
            matrix[i]=new T[cols];
        }
        return matrix;
    }
    /// <summary>
    /// Create a jagged array of rows×cols, and populate it with values row by row.
    /// </summary>
    public static T[][] CreateJaggedArray<T>(int rows, int cols, params T[] elements)
    {
        var matrix=new T[rows][];
        for(int i=0, k=0; i<rows; i++, k+=cols)
        {
            var row=new T[cols];
            Array.Copy(elements, k, row, 0, cols);
            matrix[i]=row;
        }
        return matrix;
    }
    /// <summary>
    /// Create a jagged array of rows×cols and populate it with a function like <c>(i,j)=> i==j ? 1 : 0</c>
    /// </summary>
    public static T[][] CreateJaggedArray<T>(int rows, int cols, Func<int, int, T> init)
    {
        var matrix=new T[rows][];
        for(int i=0, k=0; i<rows; i++, k+=cols)
        {
            var row=new T[cols];
            for(int j=0; j<cols; j++)
            {
                row[j]=init(i, j);
            }
            matrix[i]=row;
        }
        return matrix;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM