1

我想要实现的是将字符串拆分为多个地址,例如“NL,VENLO,5928PN”,getLocation 将返回“POINT(xy)”字符串值。

这行得通。接下来我需要为每个位置创建一个 WayPointDesc 对象。并且这些对象中的每一个都必须被推入 WayPointDesc[]。我尝试了各种方法,但到目前为止我找不到可行的选择。我最后的手段是硬编码最大数量的航路点,但我宁愿避免这样的事情。

不幸的是,使用列表不是一种选择......我认为。

这是功能:

    /* tour()
     * Input: string route
     * Output: string[] [0] DISTANCE [1] TIME [2] MAP
     * Edited 21/12/12 - Davide Nguyen
     */
    public string[] tour(string route)
    {
        // EXAMPLE INPUT FROM QUERY
        route = "NL,HELMOND,5709EM+NL,BREDA,8249EN+NL,VENLO,5928PN"; 
        string[] waypoints = route.Split('+');

        // Do something completly incomprehensible
        foreach (string point in waypoints)
        {
            xRoute.WaypointDesc wpdStart = new xRoute.WaypointDesc();
            wpdStart.wrappedCoords = new xRoute.Point[] { new xRoute.Point() };
            wpdStart.wrappedCoords[0].wkt = getLocation(point);
        }

        // Put the strange result in here somehow
        xRoute.WaypointDesc[] waypointDesc = new xRoute.WaypointDesc[] { wpdStart };

        // Calculate the route information
        xRoute.Route route = calculateRoute(waypointDesc);
        // Generate the map, travel distance and travel time using the route information
        string[] result = createMap(route);
        // Return the result
        return result;

        //WEEKEND?
    }
4

1 回答 1

5

数组是固定长度的,如果要动态添加元素,需要使用某种类型的链表结构。此外,当您最初添加它时,您的 wpdStart 变量超出了范围。

    List<xRoute.WaypointDesc> waypointDesc = new List<xRoute.WaypointDesc>();

    // Do something completly incomprehensible
    foreach (string point in waypoints)
    {
        xRoute.WaypointDesc wpdStart = new xRoute.WaypointDesc();
        wpdStart.wrappedCoords = new xRoute.Point[] { new xRoute.Point() };
        wpdStart.wrappedCoords[0].wkt = getLocation(point);

        // Put the strange result in here somehow
        waypointDesc.add(wpdStart);
    }

如果您以后真的希望列表作为数组,请使用:waypointDesc.ToArray()

于 2012-12-21T14:29:47.047 回答