在C#中,可以通過多重循環來訪問三維數組中的元素。以下是一些技巧:
int[,,] array3D = new int[3, 3, 3];
for (int i = 0; i < array3D.GetLength(0); i++)
{
for (int j = 0; j < array3D.GetLength(1); j++)
{
for (int k = 0; k < array3D.GetLength(2); k++)
{
Console.WriteLine(array3D[i, j, k]);
}
}
}
int[,,] array3D = new int[3, 3, 3];
foreach (int element in array3D)
{
Console.WriteLine(element);
}
int[,,] array3D = new int[3, 3, 3];
var query = from int element in array3D
where element % 2 == 0
select element;
foreach (int element in query)
{
Console.WriteLine(element);
}
這些技巧可以幫助您更方便地索引和操作三維數組中的元素。