[英]How would I determine if I'm on the 'edge' of a 2D array in JAVA?
So lets say that I have a 5x5 matrix 2D array and I have to determine if I am on the "edge" of the array. 因此,可以说我有一个5x5矩阵2D阵列,我必须确定我是否在阵列的“边缘”上。 As in, if I am along the "border" of the array at any point, I have reached the edge and need to end the while loop that calls on a -- what i'm assuming will be a method -- that determines this.
例如,如果我在任何时候都沿着数组的“边界”,那么我已经到达边缘,需要结束while循环,该循环调用-我假设将是一个方法-确定。 How would I go about with this scenario?
在这种情况下我将如何处理?
Test if the indices are one off from any side. 测试索引是否从任何一侧偏移一次。
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[i].length; j++)
{
if (i == 0 || j == 0 || i == matrix.length - 1 || j == matrix[i].length - 1)
{
System.out.printf("On [%d, %d] is on the border.", i, j);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.