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
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.
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?'
okay, thanks
ADDITION:
lol..okay, turns out that didn't work so well, so i used ensureminimum() but that gave me a nullpointerexception
Make a dummy "water" object at the indexes where you don't want anything else so that the array is filled.
hm, i thought that was bad programming style cause you'd be using up extra memory(even if its barely noticable).
add(int index, Object element), you can insert an element at any point in an arraylist.
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()'
Are you still using an arraylist?
ya, i want to use its index to find it location on the 'board'
Arraylists don't have a set size, also if your having lots of trouble with it you could try switching to a vector.
never heard of this vector thingy before, however, i think i'm just gonna use the dummy objects like DTBK suggested.
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.