PDA

Click to See Complete Forum and Search --> : arraylist in array/arraylist


bmwsmurf
Jan 7th, 2011, 05:42 PM
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 ! ?

TheBigB
Jan 7th, 2011, 07:55 PM
So lets assume you have an object where you store the coordinates.
The arraylist declaration would look something like this:

ArrayList<ArrayList<CoordinatesObject>> playerLand = new ArrayList<new ArrayList<CoordinatesObject>()>();

I'll check tomorrow if this actually works, but syntactically it should be correct.

bmwsmurf
Jan 8th, 2011, 04:04 AM
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 :)

TheBigB
Jan 8th, 2011, 04:20 AM
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.

bmwsmurf
Jan 8th, 2011, 09:02 AM
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:

BramVandenbon
Feb 2nd, 2011, 04:31 PM
In my opinion what you are looking for is a map ...



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

bmwsmurf
Feb 2nd, 2011, 04:34 PM
Here is the problem

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

BramVandenbon
Feb 2nd, 2011, 05:25 PM
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:


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:


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?

bmwsmurf
Feb 3rd, 2011, 11:28 AM
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