0


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,

2
  • 3
    You are looking for a Map. Though to be honest keeping data in a static manner like this is a code smell. But yea, use a Map instead. Commented Jan 6, 2013 at 15:29
  • 1
    you can use Map and "wood and pirate will be your keys and ArrayList<Waypoint> will be value associated with it. Commented Jan 6, 2013 at 15:31

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.