Results 1 to 4 of 4
  1. #1
    im_jake9 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default 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:
    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;
    		}
    	}
    }
    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 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;
    	}
    }
    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.

    Thanks,
    Jake

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!!

  3. #3
    im_jake9 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    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:
    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));
    }
    If you need my main Game class I will post it for you.

    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.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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

  1. Please help me with arrays!
    By ddrcan in forum New To Java
    Replies: 2
    Last Post: 08-10-2009, 04:30 PM
  2. Arrays
    By TheRocket in forum New To Java
    Replies: 6
    Last Post: 12-10-2008, 06:00 PM
  3. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 AM
  4. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 08:16 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM

Posting Permissions

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