JAVA:我怎样才能重新present一个二维字符数组作为使用的toString一个字符串? [英] JAVA : How can I represent a 2D character array as a String using a toString ?

查看:134
本文介绍了JAVA:我怎样才能重新present一个二维字符数组作为使用的toString一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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();
}

这篇关于JAVA:我怎样才能重新present一个二维字符数组作为使用的toString一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆