Java - 使用Point类返回2D数组中位置的值

时间:2012-09-30 06:19:39

标签: java arrays class point maze

我正在编写一个程序,其中我有一个来自单元类的2D对象数组,我需要编写一个方法,我将一个单元格对象作为参数传递,然后返回一个围绕单元格的单元格的arraylist(N ,S,E,W)。每个单元格都从Point类中获得一个位置。我遇到的问题是如何使用我所知道的位置(x,y-1为北)来生成一个单元格。对此有任何帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

如果(x,y)是你的细胞,则它周围的四个细胞是

(x+1, y)
(x-1, y)
(x, y+1)
(x, y-1)

你可以假设

+x axis is EAST
-x axis is WEST
+y axis is NORTH
-y axis is SOUTH

所以

(x+1, y) is EAST to the cell,
(x-1, y) is WEST to the cell,
(x, y+1) is NORTH to the cell and 
(x, y-1) is SOUTH 

将您作为参数传递给您的方法的单元格。