[英]How can I get the last position in this jagged array?
I have a class like this that im using as the point in the array: 我有一个这样的类,我使用作为数组中的点:
class Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
and then im trying to get the value that is closest to the end with this: 然后我试图获得最接近结束的值:
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);
}
and i have this for the point: 我有这个要点:
public Point Pos = new Point(0, 0);
Its a jagged array filled with all zeros except for a few points. 它是一个锯齿状的阵列,除了几个点外都是零。 Looks like this: 看起来像这样:
0 0 0 3 0 0 0 0 0 0 0 3 0 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
Where 2 is the closest to the end in this example. 在这个例子中,2最接近末尾。 with a position of 2,3 which i want Pos.x and Pos.y to be. 位置为2,3,我想要Pos.x和Pos.y。 It keeps giving me 0,30. 它一直给我0,30。
It looks like you are missing some curly braces. 看起来你错过了一些花括号。
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;
}
In C# the indentation is not significant so your code is being interpreted as: 在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;
You could also simplify your code considerably: 您还可以大大简化代码:
if (arr[x][y] != 0)
{
Pos.x = x;
Pos.y = y;
}
Another problem is that you are setting Pos
but printing the value of LastPos
. 另一个问题是您正在设置Pos
但是打印LastPos
的值。
If you need better performance, note that it would be faster to start searching backwards from the end and break when you hit the first non-zero element instead of searching from the start and remembering the last non-zero element you've seen. 如果你需要更好的性能,请注意,从结束开始向后搜索会更快,当你点击第一个非零元素而不是从开始搜索并记住你看到的最后一个非零元素时会更快。 This could terminate after checking just a few elements in many cases - in the best case only one element needs to be checked. 在许多情况下只检查几个元素后,这可能会终止 - 在最好的情况下,只需要检查一个元素。 So depending on the size of your data it could be hundreds of times faster to do it this way. 因此,根据数据的大小,这样做可能要快几百倍。
我不确定你的变量究竟来自哪里,但看起来你在Pos
变量中设置x / y并从LastPos
变量中读取它们。
Maybe not the best answer, but here is something done with LINQ. 也许不是最好的答案,但这是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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.