我已经创建了这样一个对象数组:
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循环打印对象数组中每个值的位置,但是我不知道如何引用这些坐标?
发布于 2014-03-03 15:35:52
您不能使用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);发布于 2014-03-03 15:35:08
您可以使用界和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]);
}
}https://stackoverflow.com/questions/22150753
复制相似问题