Results 1 to 5 of 5
  1. #1
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default Refractoring a Multi-Dinmension array

    Hi all,

    Due to the time it takes to initialise the following array
    Java Code:
    public double[][][][][][][][][] ARRAY = new double[3][3][7][7][2][33][33][2][2];
    i am trying to change int into a one dimension array using the following accessors.

    Java Code:
    public double[] ARRAY = new double[3841992];
    
    public void set_ARRAY(int sets_p1,int sets_p2,int games_p1,int games_p2,int server
    			,int total_games_p1,int total_games_p2,int win_lose_p1,int win_lose_p2,double value){
    		
    		int index = (sets_p1 + 1) * (sets_p2 +1) * (games_p1 + 1) * (games_p2 + 1) * (server + 1)
    						* (total_games_p1 + 1) * (total_games_p2 + 1) * (win_lose_p1 + 1) 
    						* (win_lose_p2 + 1);
    		this.ARRAY[index] = value;
    	}
    	public double get_ARRAY(int sets_p1,int sets_p2,int games_p1,int games_p2,int server
    			,int total_games_p1,int total_games_p2,int win_lose_p1,int win_lose_p2){
    		
    		int index = (sets_p1 + 1) * (sets_p2 +1) * (games_p1 + 1) * (games_p2 + 1) * (server + 1)
    						* (total_games_p1 + 1) * (total_games_p2 + 1) * (win_lose_p1 + 1) 
    						* (win_lose_p2 + 1);
    		
    		return this.ARRAY[index ];
    	}

    However when i switch from the original code to the new code the values returned are incorrect.

    So can anybody see an error in my logic, or can anybody see an alternate method?

    Please bear in mind that i am unable to change the size of the array or put a different structure in place as the code that assigns it values isn't mine (although i do have the source)

    Thanks in advance

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    why do you have all of this "+ 1" stuff? looks like that could be a problem.

  3. #3
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default

    the +1 is so that you won't get 'x' * 0 which would surely prove a bigger problem as anything time zero is zero!!

    As any of the original index's can be 0, this seemed the logical route!

    Maybe i'm wrong .. ..

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    oops, i didnt even notice you were multiplying everything together. i just saw a bunch of +1's.

    yeah, you're gonna need to rethink the math behind getting the correct index. just draw out a smaller example (2d or 3d), figure out the math behind it, and scale it up to whatever degree you want. i'm sure there's resources all over the internet to help you out.

    just think of it this way:

    Java Code:
    (0,0) (0,1) (0,2)
    (1,0) (1,1) (1,2)
    (2,0) (2,1) (2,2)
    should become

    Java Code:
    (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2)
      0     1     2     3     4     5     6     7     8
    you just need to find out how to convert those coordinates to the single index... then do it for a bajillion dimensions (which isn't as hard as it seems once you understand the general pattern)

  5. #5
    thorne_ is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Multi-dimensional array program-help!
    By bobmasta5 in forum New To Java
    Replies: 13
    Last Post: 03-15-2009, 07:21 PM
  2. Multi-dimensional array
    By VinTiger in forum New To Java
    Replies: 22
    Last Post: 03-01-2009, 06:51 AM
  3. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 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
  •