JAVA : How can I represent a 2D character array as a String using a toString ?

只谈情不闲聊 提交于 2019-12-08 10:05:18

问题


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.

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

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


回答1:


Is this what you mean?

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



回答2:


public String toString() {
  StringBuilder sb = new StringBuilder();
  for (int i=0; i< this.grid.length; i++) {
    for (int j=0; j < this.grid[i].length; j++) {
      sb.append(grid[i][j]);
    }
    sb.append("\n");
  }
  return sb.toString();
}



回答3:


The natural representation would matrix wise:

"-----X----O--X\n" + // i=0, j=0,...width-1
"---X----------\n" + // i=1, j=0,...width-1
...
"------XX---O--\n"   // i=height-1

Unfortunately you do not use (i, j) but (x, y). The other answers are okay.




回答4:


Do something like this

public String toString()
{
    String result = "";
    for(int i = 0; x < grid.length; i++)
    {
        for(int y = 0; y < grid[x].length; y++)
        {
            //you may need to convert each char to string here first
            result += grid[x][y];
        }
    }
    return result;
}


来源:https://stackoverflow.com/questions/8995797/java-how-can-i-represent-a-2d-character-array-as-a-string-using-a-tostring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!