Results 1 to 4 of 4
Thread: Using Arrays Within Arrays
- 11-07-2009, 05:06 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Using Arrays Within Arrays
Well, this is my first post on Java-Forums.org, so hello! :D I haven't been working with Java for a very long time, but have a good experience with JavaScript/ActionScript, HTML, and PHP.
I am working on a console (text) RPG for my first real project, and I am using Scanner for input. It's been working out well, but I've been using multidimensional arrays for the Map, and it's been getting messy. I have decided to build a Map class to store the data. This is my code so far:
My problem is that I want a function that can pass an array as a parameter to set multiple values at once, like this:PHP Code:public class Map { private Object map1[]; private Object map2[][]; private Object map3[][][]; private int depth; public Map(int depth){ map1 = new Object[depth]; map2 = null; map3 = null; this.depth = 1; } public Map(int depth1, int depth2){ map2 = new Object[depth1][depth2]; map1 = null; map3 = null; this.depth = 2; } public Map(int depth1, int depth2, int depth3){ map3 = new Object[depth1][depth2][depth3]; map1 = null; map2 = null; this.depth = 3; } public void setUnit(int coordinates[],String param){ setValue(coordinates,param); } public void setUnit(int coordinates[],int param){ setValue(coordinates,param); } public void setUnit(int coordinates[],boolean param){ setValue(coordinates,param); } private void setValue(int coordinates[], Object param){ switch(depth){ case 1: map1[coordinates[0]] = param; break; case 2: map2[coordinates[0]][coordinates[1]] = param; break; case 3: map3[coordinates[0]][coordinates[1]][coordinates[2]] = param; break; } } }
Of course, this doesn't work because map2 and map3 aren't one-dimensional arrays, and the parameter is one-dimensional. Is there a way I can make the parameter a one-, two-, or three-dimensional array? I'd rather not have to write long and tedious code with tons of method overloading if I don't have to.PHP Code:public void setUnits(String param[]){ setValues(param); } private void setValues(Object param[]){ switch(depth){ case 1: map1 = param; break; case 2: map2 = param; break; case 3: map3 = param; break; } }
Thanks,
Jake
-
If you don't mind my asking, what are you using the Map class for precisely? It seems to me that you may want to do things very differently then you currently have it set up, but I'm not sure since I am unclear on the exact requirements and planned usage of this class.
Much luck!
edit: oh, and welcome to this forum!!
- 11-07-2009, 05:23 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Thanks for the quick response!
The reason I am using it is to organize my code. I may have up to five different maps in my RPG game being used at any one time. I tried using basic arrays, but it soon got out of hand. I am building the Map class to simplify things. It is incomplete, and I will add get methods later, but I am currently working on the set methods first. If you want an example of how I want to be able to use it, here's an example:
If you need my main Game class I will post it for you.PHP Code:Map description = new Map(2,2); description.setUnits({ {"A town.", "You see a pasture in the distance."}, {"A graveyard. There is a BONE on the ground.","A desert."} }); int location[] = new int[] {0,0}; public void look(){ System.out.println(description.getUnit(location)); }
Thanks again,
Jake
P.S.
Is java usually enclosed in CODE or PHP tags on these forums? I've seen it be used differently from forum to forum.
- 11-07-2009, 07:11 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You are still describing your solution rather than the problem you want to solve.
The reason why the problem description was asked for is that there could be a better solution than the one you have already decided on. For example, it may be possible that creating a Coordinate class will help simplify matters somewhat be we can't be sure unless we know what the problem being solved is.
Similar Threads
-
Please help me with arrays!
By ddrcan in forum New To JavaReplies: 2Last Post: 08-10-2009, 04:30 PM -
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12-10-2008, 06:00 PM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM -
Help on Arrays...
By cuellar14 in forum New To JavaReplies: 4Last Post: 07-25-2008, 08:16 PM -
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11-23-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks