Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 写一个矩阵,返回周围位置的位置_Java_Arrays_Matrix_2d - Fatal编程技术网

Java 写一个矩阵,返回周围位置的位置

Java 写一个矩阵,返回周围位置的位置,java,arrays,matrix,2d,Java,Arrays,Matrix,2d,有人能给我指一下写作的方向吗。我从概念上理解这个问题,但不知道如何实现 编写包含以下字段的类MatrixNeights: 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]. 以及以下方法: Str

有人能给我指一下写作的方向吗。我从概念上理解这个问题,但不知道如何实现

编写包含以下字段的类MatrixNeights:

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>;”.
我所拥有的:

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);

    }
公共类矩阵{
int行=0;
int列=0;
int[][]矩阵;
矩阵邻居(整数行,整数列){
对于(int i=0;i如果(i你可以用数学来确定谁在附近

  • 检查给定点是否在matix内
  • 检查顶部邻居是否存在->添加到字符串
  • 检查右邻居是否存在->添加到字符串
  • 打印字符串

没有快速功能。据我所知,您应该能够通过使用try-catch块检查位置是否有效。例如,以下方法使用try-catch验证提供给该方法的位置,然后检查位置是否存在。 在所有情况下,try block都会尝试为位置设置一个值,如果它抛出一个异常,这意味着它不存在,因此不会添加到字符串中。这将最终显示有效的位置。这是我考虑过的最好的方法,但是我很确定一定有一个更简单的方法使用循环,但这取决于您进行调查

public String neighbors(int row, int column)
{
    String ret = "";
    try
    {
         matrix[row][column] = 0; // To check if the row exists we just set a dummy value.
         try { matrix[row][column - 1] = 0; ret += row + "," + (column-1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row - 1][column - 1] = 0; ret += (row-1) + "," + (column-1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row - 1][column] = 0; ret += (row-1) + "," + column + ";"; }
         catch (Exception e) {} 
         try { matrix[row - 1][column + 1] = 0; ret += (row-1) + "," + (column+1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row][column + 1] = 0; ret += row + "," + (column+1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row + 1][column + 1] = 0; ret += (row+1) + "," + (column+1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row + 1][column] = 0; ret += (row+1) + "," + column + ";"; }
         catch (Exception e) {} 
         try { matrix[row + 1][column - 1] = 0; ret += (row+1) + "," + (column-1) + ";"; }
         catch (Exception e) {} 
    }
    catch (Exception e)
    {
        ret += "Cell does not exist."; // Woho, exception caused due to position non-existing in matrix.
    }
    finally
    {
        return ret;
    }
}
public String neighbors(int row, int column)
{
    String ret = "";
    try
    {
         matrix[row][column] = 0; // To check if the row exists we just set a dummy value.
         try { matrix[row][column - 1] = 0; ret += row + "," + (column-1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row - 1][column - 1] = 0; ret += (row-1) + "," + (column-1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row - 1][column] = 0; ret += (row-1) + "," + column + ";"; }
         catch (Exception e) {} 
         try { matrix[row - 1][column + 1] = 0; ret += (row-1) + "," + (column+1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row][column + 1] = 0; ret += row + "," + (column+1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row + 1][column + 1] = 0; ret += (row+1) + "," + (column+1) + ";"; }
         catch (Exception e) {} 
         try { matrix[row + 1][column] = 0; ret += (row+1) + "," + column + ";"; }
         catch (Exception e) {} 
         try { matrix[row + 1][column - 1] = 0; ret += (row+1) + "," + (column-1) + ";"; }
         catch (Exception e) {} 
    }
    catch (Exception e)
    {
        ret += "Cell does not exist."; // Woho, exception caused due to position non-existing in matrix.
    }
    finally
    {
        return ret;
    }
}