我对多路点的ArrayList有问题。我只有一艘船。一艘船有航路。
public static ArrayList<Waypoint> _waypoints = new ArrayList<>();若要添加新的路径点,请使用
Screen._waypoints.add(
new Waypoint(
12,20
)
);
Screen._waypoints.add(
new Waypoint(
15,50
)
);
Screen._waypoints.add(
new Waypoint(
17,90
)
);这意味着:
我已经修改了我的游戏,我增加了船类型,这意味着每种类型的船都有不同的路径点。
我修改了路径点初始化。
public static ArrayList<ArrayList<Waypoint>> _waypoints = new ArrayList<ArrayList<Waypoint>>();我想要创建这样的结构:
船舶->木->阵列路点列表
例如,我有两种类型的船->木材和海盗船。
->木船
->海盗船
为了获得木材的arrayList,我想使用以下方法:
waypoints.get("wood");我不知道如何用二维arrayList of arrayList实现它
谢谢,
发布于 2013-01-06 15:30:54
你在找Map。
public static Map<String, List<Waypoint>> wayPoints = new HashMap<String, List<Waypoint>>();不过,更好的方法是创建您自己的ShipType类,并在船本身上存储一个路径点列表。更有可能的是,您将拥有更多特定于一种ship类型的属性。这使您可以将这些合并到一个类中,从而实现一个更易于管理的设计。
public class ShipType {
private List<Waypoint> wayPoints = new ArrayList<Waypoint>();
/* ... */
}然后,您的Ship可以有一个ShipType,而不是其ship类型的“仅”名称。
public class Ship {
private ShipType type;
/* ... */
}然后,只需保持Map的ShipType,以正确地构造您的Ship。
public static Map<String, ShipType> ships = new HashMap<String, ShipType>();
// Register ship types
ships.put("wood", new WoodShipType());
// Construct a ship
Ship myShip = new Ship();
myShip.setType(ships.get("wood"));或者,您可以使用带有重载方法的enum来表示固定数量的ship类型,并完全摆脱该static集合。
发布于 2013-01-06 15:31:07
使用Map怎么样?
Map<String, List<WayPoint>> wayPoints = new HashMap<String, List<WayPoint>>();
wayPoints.put("wood", new ArrayList<WayPoint>());然后通过以下方法获得木材的arrayList:
List<WayPoint> woods = wayPoints.get("wood");发布于 2013-01-06 15:35:42
您可以使用HashMap
public HashMap<String,ArrayList<Waypoint>> waypoints=new HashMap<String,ArrayList<Waypoint>>();
waypoints.put("wood",array list objetct); //insertion
ArrayList<Waypoints> obj=waypoints.get("wood");https://stackoverflow.com/questions/14183580
复制相似问题