簡體   English   中英

多維鋸齒形數組初始化

[英]multi dimensional jagged array initialization

我正在使用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";
    }
}

要初始化一個二維鋸齒狀數組,您需要一個循環。

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

當然,最好創建一個函數來初始化各種尺寸的鋸齒狀數組以供代碼重用。

例如,這是我庫中用於二維鋸齒陣列的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM