Question


I have a problem with a multiple ArrayList of waypoints. I have one ship. A ship has waypoints.

public static ArrayList<Waypoint> _waypoints = new ArrayList<>();

To add a new waypoint I use

 Screen._waypoints.add(
                        new Waypoint(
                           12,20
                        )
                   );
 Screen._waypoints.add(
                        new Waypoint(
                           15,50
                        )
                   );
 Screen._waypoints.add(
                        new Waypoint(
                           17,90
                        )
                   );

That means:

  1. Ship -> 12,20
  2. Ship -> 15,50
  3. Ship -> 17,90

I have modified my game and I have added ship types that means every type of ship has a different waypoints.

I modified the waypoints initialization.

public static ArrayList<ArrayList<Waypoint>> _waypoints = new ArrayList<ArrayList<Waypoint>>();

I want to create this structure:
Ship -> Wood -> Array list of waypoints
For example I have two types of ship -> Wood and Pirate ship.

Ship -> Wood

  1. Ship -> 12,20
  2. Ship -> 15,50
  3. Ship -> 17,90

Ship -> Pirate

  1. Ship -> 12,20
  2. Ship -> 15,50
  3. Ship -> 17,90

To get the arrayList of the wood I want to use this:

waypoints.get("wood");

I don't know how to realize it using a two dimensional arrayList of arrayList

Thanks,

Was it helpful?

Solution

You're looking for a Map.

public static Map<String, List<Waypoint>> wayPoints = new HashMap<String, List<Waypoint>>();

Although, a better approach would be to make your own ShipType class and store a list of waypoints on the ship itself. More than likely, you'll have many more properties which are specific to one ship type. This allows you to consolidate those in a single class, making for a much more manageable design.

public class ShipType {
    private List<Waypoint> wayPoints = new ArrayList<Waypoint>();
    /* ... */
}

Your Ships could then have a ShipType instead of "just" the name of their ship type.

public class Ship {
    private ShipType type;
    /* ... */
}

Then, simply keep a Map of your ShipTypes to properly construct your Ships.

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"));

Alternatively, you can use an enum with overloaded methods to represent a fixed number of ship types and get rid of that static collection altogether.

OTHER TIPS

How about using a Map?

Map<String, List<WayPoint>> wayPoints = new HashMap<String, List<WayPoint>>();
wayPoints.put("wood", new ArrayList<WayPoint>());

And then get the arrayList of the wood by:

List<WayPoint> woods = wayPoints.get("wood");

You can use 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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top