【发布时间】:2015-09-22 14:25:57
【问题描述】:
谁能指出我的写作方向。我从概念上理解这个问题,但不知道如何实现。
使用以下字段编写一个类 MatrixNeighbors:
int rows
int columns
int[][] matrix
以下构造函数:
MatrixNeighbors(int rows, int columns) : sets both the rows and columns fields, then creates a new matrix of size [rows]x[columns].
还有以下方法:
String neighbors(int row, int column) : returns a String containing all the cell neighbors of the cell at row, column, starting directly above the specified cell and moving clockwise. If the cell is along any edge, be sure to omit the non-existent neighbors. See below for formatting.
Strings will be formatted as such: “<above cell row>,<above cell column>;<next cell clockwise row>,<next cell clockwise column>;…<last cell row>,<last cell column>;”.
通过返回字符串“cell不存在”来处理不存在的单元格,即存在于矩阵边界之外的单元格。
考虑下面的图表和代码:
(0,0) (1,0) (2,0)
(0,1) (1,1) (2,1)
(0,2) (1,2) (2,2)
MatrixNeighbors m = new MatrixNeighbors(3, 3);
System.out.println(m.neighbors(0, 0)); //prints "0,1;1,1;1,0;"
System.out.println(m.neighbors(2, 2)); //prints "1,2;2,1;1,1;"
System.out.println(m.neighbors(1, 1)); //prints "0,1;0,2;1,2;2,2;2,1;2,0;1,0;0,0;"
System.out.println(m.neighbors(3, 0)); //prints "cell does not exist"
我有什么:
public class MatrixNeighbors {
int rows = 0;
int columns = 0;
int [][] matrix;
MatrixNeighbors(int row, int column) {
for (int i = 0; i < MatrixNeighbors.length; i++)
for (int j = 0; j < MatrixNeighbors[i].length; j++)
if (i <= row && j <= column)
return (row - 1, column);
}
我相信这是现在返回点的位置在该点上方的一行,但我不知道如何确保它仅在该点位于矩阵内时才返回它,然后如何将该点返回到新地点的右边。
【问题讨论】: