[英]How would I determine if I'm on the 'edge' of a 2D array in JAVA?
因此,可以說我有一個5x5矩陣2D陣列,我必須確定我是否在陣列的“邊緣”上。 例如,如果我在任何時候都沿着數組的“邊界”,那么我已經到達邊緣,需要結束while循環,該循環調用-我假設將是一個方法-確定。 在這種情況下我將如何處理?
測試索引是否從任何一側偏移一次。
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.