【问题标题】:Why is my method never returning anything but null?为什么我的方法只返回空值?
【发布时间】:2014-04-30 15:39:51
【问题描述】:

重要编辑:我忘了实际发布一开始调试所需的大量代码。以下是链接:

http://pastebin.com/rxENqzBB

http://pastebin.com/KVXCfys4

http://pastebin.com/Sgv01P8V

http://pastebin.com/mrNVej67

我目前正在使用 ADT IDE 为 Android 4.3 制作 Java 应用程序。该应用程序包含一个校园地图,并向用户输出从他们当前所在的房间到他们希望去的房间的方向(两者都已由用户输入)。但是,我遇到了一个问题 - 以下方法总是返回 null,即使它不应该这样做:

Coordinates findCoordsWithRoom(String roomName)
{
    //all floors; 0 -> 1
    for (int z = 0; z < map.length; z++)
    {
        //all rows; 0 -> 10
        for (int y = 0; y < map[z].length; y++)
        {
            //all cols; 0 -> 9
            for (int x = 0; x < map[z][y].length; x++)
            {
                if (dne.equals(map[z][y][x])) { continue; } //if this node is a "dne" node, skip it
                //all valid connections out of this node
                for (int c = 0; c < map[z][y][x].dirArr.length; c++)
                {
                    if (!map[z][y][x].dirArr[c].exists) { continue; }
                    //all rooms on this connection
                    for (int r = 0; r < map[z][y][x].dirArr[c].rooms.length; r++)
                    {
                        String thisRoomName = map[z][y][x].dirArr[c].rooms[r].name;
                        //if this room has the right name
                        if (roomName.equals(thisRoomName))
                        {   //found it
                            return map[z][y][x].coords;
                        }
                    }
                }
            }
        }
    }
    //failed to find it
    return null;
}

即使我的调试器告诉我相关变量是相等的,if(roomName.equals(thisRoomName)) 也永远不会计算为真,因为程序永远不会命中其中的 return 语句。

这是map 变量:

Node[][][] map =
{
    {   //first floor
        //00   01   02   03   04   05   06   07   08   09 <-x/y:
        {dne, dne, dne, dne, dne, cls, dne, dne, dne, dne}, //00
        {dne, dne, dne, dne,  oc,  cl,  cf, dne, dne, dne}, //01
        {dne, dne, dne, dne, dne, dne, dne, dne, dne, dne}, //02
        {dne, dne, dne, dne, og2,  g4,  gc,  g2, gs1, dne}, //03
        {dne, dne, dne, dne, dne, dne, gs2,  g1,  g3, dne}, //04
        {dne, ot3, dne, ot1,  op, dne, og1, gal, dne, dne}, //05
        {dne, ot4, dne, dne, dne, dne,  a2,  a1, dne, dne}, //06
        {dne,  t3, dne, dne, dne, dne, dne,  a3,  as, dne}, //07
        {dne,  t1, tl1, ot2, dne, dne, dne,  a4, dne, dne}, //08
        { ts,  t2, tl2, ot5, dne, dne, dne, dne, dne, dne}, //09
        {dne,  t4, tls, dne, dne, dne, dne, dne, dne, dne}, //10
    },
    {   //second floor
        //00   01   02   03   04   05   06   07   08   09 <-x/y:
        {dne, dne, dne, dne, dne, CLS, dne, dne, dne, dne}, //00
        {dne, dne, dne, dne, dne,  CL,  C2,  C4, dne, dne}, //01
        {dne, dne, dne, dne, dne,  C1,  C3,  C5, dne, dne}, //02
        {dne, dne, dne, dne, dne,  G4, GC1,  G3, GS1, dne}, //03
        {dne, dne, dne, dne, dne, GC2, dne,  G1,  G2, dne}, //04
        {dne, dne, dne, dne, dne, GS2, dne, GAL, dne, dne}, //05
        {dne, dne, dne, dne, dne, dne,  A5,GALS, dne, dne}, //06
        {dne, dne, dne, dne, dne, dne,  A3,  A1, dne, dne}, //07
        {dne, dne, dne, dne, dne, dne, dne,  A2,  AS, dne}, //08
        { TS,  T4,  T3, dne, dne, dne, dne, dne, dne, dne}, //09
        {dne, TLS,  TL, dne, dne, dne, dne, dne, dne, dne}, //10
    }
};

dne 只是static Node dne = new Node();

这里是map中所有对象的结构的链接,因为太大了,无法直接粘贴:http://pastebin.com/Qad46gHh

Coordinates 对象本质上只是三个ints - xyz 的容器,并由public Coordinates (int z, int y, int x) 构造。

这是调用方法的地方:

public ArrayList<Coordinates> makeRoute(String start, String end)
{
    destination = end;
    ArrayList<Coordinates> newCoordArr1 = new ArrayList<Coordinates>();
    ArrayList<Coordinates> newCoordArr2 = new ArrayList<Coordinates>();
    ArrayList<Coordinates> newCoordArr3 = new ArrayList<Coordinates>();
    Coordinates startCoords = world.findCoordsWithRoom(start);
    Coordinates endCoords = world.findCoordsWithRoom(end);
    ArrayList<Coordinates> path = world.makePath(startCoords, endCoords, newCoordArr1, newCoordArr2, newCoordArr3);
    return path;
}

例如,start 是“104”,end 是“204”(分别附加到 g2G2),但 startCoordsendCoords 都是 null

这里是寻路算法的链接:http://pastebin.com/NVhSU06w

为了避免出现垃圾问题以引起人们的注意,我想郑重声明,我的问题历史记录中的其他两个问题虽然都与同一个项目有关,但都与我在上述项目中遇到的不同问题。

【问题讨论】:

  • 我在您的地图中既没有看到“104”也没有看到“204”。
  • 204只是一个dne房间,那么如果你要找的房间是204,代码会跳过它
  • @xgc1986 澄清了我的意思
  • @Karakuri 澄清了我的意思
  • 保留所有房间的列表不是更简单吗?

标签: java android multidimensional-array adt path-finding


【解决方案1】:

wouldn't it be simpler to keep a list of all the rooms? – njzk2 Apr 30 at 16:10

好吧,我遵循了这个建议(制作了一个自定义 Pair 类的 ArrayList,并在其中存储了预期的输入输出),至少项目的这个问题得到了解决。不幸的是,我仍然不知道问题出在哪里,但至少它的一部分是有效的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多