函数返回char时没有返回任何内容

时间:2016-06-17 22:23:36

标签: java arrays char

在我写的程序中,我有一个名为Map的类,在该类中是一个名为coord的函数,它返回给定点的地图坐标。

Map类定义:

public class Map
{
    public static char map[][] = new char[150][150];

    public static char coord(Point locat)
    {
            if ((locat.v <= 0) || (locat.h <= 0))
                    return '\u0000';
            return map[locat.v-1][locat.h-1];
    }

    public static int readToArray(String filePath)
    {
            try (InputStream in = new FileInputStream(filePath);
                 BufferedReader from = new BufferedReader(new InputStreamReader(in))) {
                    int c, h, v;
                    h = v = 0;

                    while ((c = from.read()) != -1) {
                        if (c == '\n') {
                            v++;
                            h = 0;
                        } else {
                            map[v][h++] = (char) c;
                        }
                    }

                    in.close();
                    return v;

             } catch (IOException x) {
                    System.err.println(x);
             }
             return -1;
    }
    /* blah blah blah */
}

Point类定义:

public class Point
{
    public int h, v;
    /* blah blah blah */
}

Map类是静态的。我的main函数调用readToArray

我添加printf以查找coord函数通过错误检查但返回的char为空。为什么这个以及我该怎么做才能改变它?

1 个答案:

答案 0 :(得分:0)

如果索引不正确,您从数组中检索值的函数将返回\u0000。我认为,char在打印时不显示任何内容,甚至不显示空格。因此,它正在打印一个char,只是一个实际上并不意味着显示任何东西。

相关问题