多维锯齿状数组初始化

时间:2018-04-26 16:15:10

标签: c# xamarin.android

我正在使用NFC mifare读卡器,我正在尝试创建这样的字节数组,但是我收到错误

int FIX_SECTOR_COUNT = 16;

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

错误CS0178无效的排名说明符:预期','或']

所以我做了这个

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

但我需要帮助实例化它。

我需要字节数组来做这样的事情:

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";
    }
}

1 个答案:

答案 0 :(得分:2)

要初始化2D锯齿状阵列,您需要一个循环。

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

现在将其扩展为3D锯齿状数组

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;
}

当然,最好创建一个函数来初始化各种维度的锯齿状数组,以便重复使用代码。

例如,这是我的库中有关于2D锯齿状数组的代码:

    /// <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;
    }