C# 如何获得这个锯齿阵列中的最后一个位置?

C# 如何获得这个锯齿阵列中的最后一个位置?,c#,C#,我有一个这样的类,我用它作为数组中的点: class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } 然后,我试图通过以下方法获得最接近结尾的值: public void Position() {

我有一个这样的类,我用它作为数组中的点:

class Point
    {
        public int x;
        public int y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
然后,我试图通过以下方法获得最接近结尾的值:

public void Position()
        {
            for (int x = 0; x < arr.Length; x++)
            {
                for (int y = 0; y < arr[x].Length; y++)
                {
                    if (arr[x][y] == 3)
                        Pos.x = x;
                        Pos.y = y;
                    if (arr[x][y] == 1)
                        Pos.x = x;
                        Pos.y = y;
                    if (arr[x][y] == 2)
                        Pos.x = x;
                        Pos.y = y;
                }
            }
            Console.Write("Last Position: {0}, {1}",LastPos.x, LastPos.y);
        }
它是一个锯齿状数组,除了几个点之外,全是零。看起来像这样:

0 0 3 0 0 0 0

0 0 1 0 0 0 0

0 0 2 0 0 0 0 0

0 0 0 0

0 0 0 0 0 0


其中,在本例中,2是最接近端点的。位置为2,3,我希望位置x和位置y为。它一直给我0,30。

看起来你少了一些花括号

if (arr[x][y] == 3)
{
    Pos.x = x;
    Pos.y = y;
}
if (arr[x][y] == 1)
{
    Pos.x = x;
    Pos.y = y;
}
if (arr[x][y] == 2)
{
    Pos.x = x;
    Pos.y = y;
}
在C#中,缩进并不重要,因此您的代码被解释为:

if (arr[x][y] == 3)
{
    Pos.x = x;
}
Pos.y = y;
if (arr[x][y] == 1)
{
    Pos.x = x;
}
Pos.y = y;
if (arr[x][y] == 2)
{
    Pos.x = x;
}
Pos.y = y;
您还可以大大简化代码:

if (arr[x][y] != 0)
{
    Pos.x = x;
    Pos.y = y;
}
另一个问题是,您正在设置
Pos
,但正在打印
LastPos
的值


如果您需要更好的性能,请注意,当您点击第一个非零元素时,从末尾开始向后搜索并中断搜索会更快,而不是从一开始就搜索并记住您看到的最后一个非零元素。在许多情况下,这可能在只检查几个元素后终止-在最好的情况下,只需要检查一个元素。因此,根据数据的大小,这样做的速度可能快几百倍。

我不确定变量的确切来源,但看起来您正在
Pos
变量中设置x/y,并从
LastPos
变量中读取它们。

可能不是最佳答案,但是这里有一些关于LINQ的事情

int[][] array = new int[][] 
{
    new int[] {0, 0, 0, 3, 0, 0, 0, 0},
    new int[] {0, 0, 1, 0, 0, 0, 0},
    new int[] {0, 0, 0, 2, 0, 0, 0, 0 },
    new int[] {0, 0, 0, 0, 0 },
    new int[] {0, 0, 0, 0, 0, 0, 0 }
};

var query = (from row in array.Select((r, idx) => new { r, idx })
             where row.r.Any(item => item != 0)
             select new
             {
                 Row = row.idx,
                 Column = (from item in row.r.Select((i, idx) => new { i, idx })
                           where item.i != 0
                           select item.idx).Last()
             }).Last();

Console.WriteLine("{0}\t{1}", query.Row, query.Column);

是的,我是个白痴,忘记戴牙套是很悲哀的。我也用了简化版。
int[][] array = new int[][] 
{
    new int[] {0, 0, 0, 3, 0, 0, 0, 0},
    new int[] {0, 0, 1, 0, 0, 0, 0},
    new int[] {0, 0, 0, 2, 0, 0, 0, 0 },
    new int[] {0, 0, 0, 0, 0 },
    new int[] {0, 0, 0, 0, 0, 0, 0 }
};

var query = (from row in array.Select((r, idx) => new { r, idx })
             where row.r.Any(item => item != 0)
             select new
             {
                 Row = row.idx,
                 Column = (from item in row.r.Select((i, idx) => new { i, idx })
                           where item.i != 0
                           select item.idx).Last()
             }).Last();

Console.WriteLine("{0}\t{1}", query.Row, query.Column);