JAVA:我怎样才能重新present一个二维字符数组作为使用的toString一个字符串?数组、字符串、字符、JAVA

2023-09-07 14:14:45 作者:℡﹏埖埖乄丗界

我正在使用LRV程序(最低最近访问)算法。 基本上,我设计该算法用于机器人通过网格遍历(它是一个二维字符数组)。同时遍历网格机器人检查每个单元是否是空的(由定义 - ),被占领(由O的定义)或BLOCKED(由X中定义)。 细胞只能由被称为传感器的物体所占据(这有它自己的类)。 阻挡的小区不能移动的。 每次机器人必须移动,它接收来自所述传感器的方向。因此,在开始时,机器人将被放置在网格上,它会下降的传感器,并从它得到的方向,或者从一个$ P $对现有传感器的方向。

I am making a program using the LRV(Least recently visited) Algorithm. Basically, I design the algorithm for a robot to traverse through a grid (which is a 2D char array). The robot whilst traversing the grid checks whether each cell is either EMPTY (defined by '-'), OCCUPIED ( defined by 'O' ) or BLOCKED (defined by 'X'). The cells can only be occupied by an object known as Sensor (this has its own class). BLOCKED cells cannot be traversed on. Each time the robot must move, it receives a direction from the sensor. So in the beginning the robot would be placed on the grid and it would drop a sensor and get a direction from it, or get a direction from a pre-existing sensor.

现在,我已经解释了我的计划, 我的具体问题是,我有我的GridMap类

Now that I've explained my program, my specific question is, I have my GridMap class

public class GridMap {
  private int height;
  private int width;
  private char[][] grid;


  public GridMap(int x, int y) { 
    height=x;
    width=y;  
    grid = new char [x][y];
  }
  public int getHeight(){
    return height;
  }
  public int getWidth(){
    return width;
  }
  public String toString(){
    String s1 = 
    return s1;
  }
  public char getElementAt(int x, int y){
  }
  public void setElementAt(int x, int y){
  }
  public boolean isCellBlocked(int x, int y){
  }
  public double getCoverageIndex(){
    return COVERAGE_INDEX;
  }
}

我想知道的是我怎么能重新present我的2D字符数组的字符串 - ,澳及X。 我试图将尽可能详细,如果任何人有任何问题,我会很乐意尽快回答。 任何帮助将是非常美联社preciated。 谢谢

What I want to know is how can I represent my 2D char array as a string of -, O's and X's. I tried to be as detailed as possible, if anyone has any questions I'd be willing to answer asap. Any help will be highly appreciated. Thanks

Varun的

推荐答案

这是什么意思?

public String toString()
{
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < getHeight(); i++)
    {
        for(int j = 0; j < getWidth(); j++)
        {
            builder.append(grid[i][j]);
        }
    }    
    return builder.toString();
}
 
精彩推荐
图片推荐