Results 1 to 9 of 9

Thread: arraylist in array/arraylist

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    45

    Angry arraylist in array/arraylist

    Hi,
    So first gonne tell the situation over here,
    I want to store coordinates for players,
    the coordinates i get trough by bp.getx() and bp.getz()
    those 2 coordinates makes a block only the player can buy more land so more coordinates need to be easly added

    so i tought about 2 arraylists 1 for the players and then i want 1 arraylist for each player... only how do i do that and how can i store that ! ?

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: arraylist in array/arraylist

    So lets assume you have an object where you store the coordinates.
    The arraylist declaration would look something like this:

    Code:
    ArrayList<ArrayList<CoordinatesObject>> playerLand = new ArrayList<new ArrayList<CoordinatesObject>()>();
    I'll check tomorrow if this actually works, but syntactically it should be correct.
    Delete it. They just clutter threads anyway.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    45

    Re: arraylist in array/arraylist

    why an object ?

    also if there are easyer ways to store this date (to store the coordinates for each player but easly to add coordinates to a player) pleas tell me
    Last edited by bmwsmurf; Jan 8th, 2011 at 05:18 AM.

  4. #4
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: arraylist in array/arraylist

    Well, you need two coordinates per block per person, right?
    You can build a very simple class to hold those coordinates in a block list in a playerlist.
    Delete it. They just clutter threads anyway.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    45

    Re: arraylist in array/arraylist

    ye 2 coordinates each player only the players can add coordinates so it could look like

    playername: x z : x z : x z : x z:
    playername2: x z : x z : x z : x z:
    playername1: x z : x z : x z : x z:

  6. #6
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: arraylist in array/arraylist

    In my opinion what you are looking for is a map ...

    Code:
    Map map = new HashMap();
    
    List list1 = new ArrayList();
    map.put("player1", list1);
    
    List list2 = new ArrayList();
    map.put("player2", list2);
    
    // later when you want the list of a certain player you can get it as follows:
    List list = map.get("player1");
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    45

    Re: arraylist in array/arraylist

    Here is the problem

    i dont know how many players there will be i dont want to work in the code later

  8. #8
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: arraylist in array/arraylist

    My previous example of course was only to demonstrate how maps work.
    Of course you don't have to hard-code the player names.

    If you use a list or a map, in both cases the names have to come from somewhere, in my examples I can only type them as strings, but of course in reality they should come from user input or a configuration file , or whatever that speaks for itself.

    A map is pretty much like an array list. It has no maximum number of items, and you can loop through it as follows:

    Code:
          String playerName;
          List list;
          Iterator iterator = map.keySet().iterator();
          while (iterator.hasNext())
          {
            playerName = (String) iterator.next();
            list = (List) map.get(playerName);
          }
    Alternative solution

    But hey, on the other side, have you concidered keeping a list of players while storing their properties within the player objects? It seems to me that the lands of a player could perfectly be regarded as a property of the players themself.

    List playerList = new ArrayList();

    and then just fill it with objects that look like this:

    Code:
    public class Player
    {
      private List bpList = new ArrayList();
    
      public List getBpList()
      {
        return bpList;
      }
    }
    I just don't see an arraylist in an arraylist as a good data model it's bad design on so many levels.
    By the way, I do agree that coordinates are best stored as objects. I assumed that was wat the bp in your original post was supposed to be. bp is an instance of a class BlockPoint or something, right?
    Last edited by BramVandenbon; Feb 2nd, 2011 at 06:33 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    45

    Re: arraylist in array/arraylist

    bp is an object or i think

    so i can do:

    bp.getx();
    bp.getz();

    those to have to be combined and put away when asked to... the player can have multiplyed [x,z] coords

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width