Staredit Network

Staredit Network -> Computers and Technical -> Java Help
Report, edit, etc...Posted by Zeratul_101 on 2007-01-04 at 18:10:01
okay, heres the deal. i'm making a risk-like game with territories represented by a matrix/multi-dimensional array. some elements of the array will represent water and have no function. the elements that represent land will each have a nation object. now, i'm storing the objects in an ArrayList and i'm using the index of these objects to represent their location on the matrix(see pic). the issue is that it won't let me insert that element at any point(the only valid insertion point is the lowest empty index).

xxx
xxx
xxx

x = index 5

xxx
xxx
xxx

x = index 9

so, is there a way to get around this? or should i use another method of storing the location data?

ps, it throws an indexoutofbounds exception

Report, edit, etc...Posted by CheeZe on 2007-01-04 at 18:16:21
Create a multi-dimension array. Create arraylists inside the arraylist for a two dimensional matrix. Then, the coordinates would just be like (x,y) of any graph.
Report, edit, etc...Posted by Zeratul_101 on 2007-01-04 at 18:31:05
thats nice to know, but it still doesn't solve my problem. i'm not making an object for each index, only at certain indexes, which are 'out of bound.' the question can also be seen as 'how do you set the size for an arraylist?'
Report, edit, etc...Posted by CheeZe on 2007-01-04 at 18:44:53
CODE
ArrayList array = new ArrayList(int size)

Refer to Java's API:
http://java.sun.com/j2se/1.5.0/docs/api/
Report, edit, etc...Posted by Zeratul_101 on 2007-01-06 at 15:33:43
okay, thanks

ADDITION:
lol..okay, turns out that didn't work so well, so i used ensureminimum() but that gave me a nullpointerexception pinch.gif
Report, edit, etc...Posted by CheeZe on 2007-01-07 at 22:26:37
Show the code.
Report, edit, etc...Posted by DT_Battlekruser on 2007-01-08 at 00:56:00
Make a dummy "water" object at the indexes where you don't want anything else so that the array is filled.
Report, edit, etc...Posted by Zeratul_101 on 2007-01-08 at 01:08:17
hm, i thought that was bad programming style cause you'd be using up extra memory(even if its barely noticable).
Report, edit, etc...Posted by Vibrator on 2007-01-08 at 18:39:39
add(int index, Object element), you can insert an element at any point in an arraylist.
Report, edit, etc...Posted by Zeratul_101 on 2007-01-08 at 19:04:44
i'm way past that point, its giving me an 'outofindex' exception. i can't seem to change the size of the array when i'm instantiating it and it gives me a nullpointer exception when i use 'ensureminimum()'
Report, edit, etc...Posted by Vibrator on 2007-01-08 at 21:01:29
Are you still using an arraylist?
Report, edit, etc...Posted by Zeratul_101 on 2007-01-08 at 21:04:04
ya, i want to use its index to find it location on the 'board'
Report, edit, etc...Posted by Vibrator on 2007-01-08 at 21:30:42
Arraylists don't have a set size, also if your having lots of trouble with it you could try switching to a vector.
Report, edit, etc...Posted by Zeratul_101 on 2007-01-08 at 23:28:17
never heard of this vector thingy before, however, i think i'm just gonna use the dummy objects like DTBK suggested.
Report, edit, etc...Posted by DT_Battlekruser on 2007-01-09 at 01:46:51
QUOTE(Zeratul_101 @ Jan 7 2007, 10:08 PM)
hm, i thought that was bad programming style cause you'd be using up extra memory(even if its barely noticable).
[right][snapback]611301[/snapback][/right]


Unless you have some extremely important obligation to size, it's good programming etiquette to include a dummy element for water, since it makes things easier to go back and edit. Notice that if you look at large applications, everything is stored in classes. This might take more bytes, but it is highly organized when compared to other routines.
Next Page (1)