[英]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 我正在使用NFC mifare读卡器,并试图创建这样的字节数组,但出现错误
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 ']
错误CS0178无效的排名说明符:预期为','或']
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 现在将其扩展到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;
}
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.