我已经创建了一个对象数组:
object[,] Values = new object[17, 5];
Layer1[0, 0] = neuron1;
Layer1[1, 0] = neuron1;
Layer1[2, 0] = neuron2;
等
我编写了一个循环遍历对象数组的函数:
static void Loop_Through_Layer(object[,] Layer1)
{
//Loop through objects in array
foreach (object element in Layer1)
{
if (element != null)
{
//Loop through objects in array
foreach (object index in Layer1)
{
if (index != null)
{
//Need to print indexes of values
Console.ReadLine();
}
}
}
}
}
我试图这样做,所以通过使用for循环打印对象数组中每个值的位置,但我不确定如何引用这些坐标?
答案 0 :(得分:3)
您可以使用边界和for
循环来获取值:
for (int x = 0; x < Layer1.GetLength(0); x++)
{
for (int y = 0; y < Layer1.GetLength(1); y++)
{
//
// You have position x and y here.
//
Console.WriteLine("At x '{0}' and y '{1}' you have this value '{2}'", x, y, Layer1[x, y]);
}
}
答案 1 :(得分:1)
你不能使用foreach
因为它“扁平化”数组 - 你可以使用两个普通的for
循环:
//Loop through objects in array
for(int i = 0; i < Layer1.GetLength(0); i++)
{
for(int j = 0; j < Layer1.GetLength(1); j++)
{
var element = Layer1[i,j];
if (element != null)
{
//Need to print indexes of values
Console.WriteLine("Layer1[{0},{1}] = {2}", i, j, element);