aspose file tools*
The moose likes Beginning Java and the fly likes Int array to 3D array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Int array to 3D array" Watch "Int array to 3D array" New topic
Author

Int array to 3D array

Karoline Lim
Greenhorn

Joined: Oct 14, 2011
Posts: 12
Hi, I've an array of tCost=0 2 3 3 0 1 2 1 0 0 5 2 1 0 3 3 2 0 0 2 1 3 0 1 5 4 0 0 1 3 2 0 1 2 2 0, and I would like to map it into 3 dimensional matrix, tCost1[j][i][k]



I wonder what is the correct way of doing this, because the output I got is,

0 0 0
0 0 0
0 0 0

0 0 0
0 0 0
0 0 0

0 0 0
0 0 0
0 0 0

0 0 0
0 0 0
0 0 0

0 0 0
0 0 0
0 0 0

Thanks in advance!
Karoline Lim
Greenhorn

Joined: Oct 14, 2011
Posts: 12
I've found out the reason already.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
And how did you sort it out?

By the way: you do realise there is no such thing as a multi-dimensional array in Java™? It only supports arrays of arrays. You are dealing with an array of arrays of arrays.
And where did you get 36 from? You are printing 45 0s. You need to link the size of the “target” array to the size of the “source” array. That is why there are style recommendations (§10.3) against using numbers like 36, 3 and 5 in your code.

This sort of code cannot overrun the bounds of the array:Note that this suggestion will discard 8 values if your original String contains 44 tokens. You can of course add a final array of this sort to use those last 8, if you so wish:
1 2 3
4 5 6
7 8
Karoline Lim
Greenhorn

Joined: Oct 14, 2011
Posts: 12
Campbell Ritchie wrote:And how did you sort it out?

By the way: you do realise there is no such thing as a multi-dimensional array in Java™? It only supports arrays of arrays. You are dealing with an array of arrays of arrays.
And where did you get 36 from? You are printing 45 0s. You need to link the size of the “target” array to the size of the “source” array. That is why there are style recommendations (§10.3) against using numbers like 36, 3 and 5 in your code.

This sort of code cannot overrun the bounds of the array:Note that this suggestion will discard 8 values if your original String contains 44 tokens. You can of course add a final array of this sort to use those last 8, if you so wish:
1 2 3
4 5 6
7 8


I thought I did, but when I checked the arrays of arrays out, the values are wrong..

Thanks for the comments, and I did try your codes, and changed the for loop into 36 tokens (4,3,3)



and it generates the token value right (1 to 36).

But when the line 16 is unmasked, I got output like this,

Token value= 1
Token value= 3
Token value= 5
Token value= 7
Token value= 9
Token value= 11
Token value= 13
Token value= 15
Token value= 17
Token value= 19
Token value= 21
Token value= 23
Token value= 25
Token value= 27
Token value= 29
Token value= 31
Token value= 33
Token value= 35
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 36
at alsClass.main(alsClass.java:245)

why would it be this??

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
There is a big mistake. It shouldn’t be tokens.length / COLUMNS - ROWS. It should be tokens.length / COLUMNS / ROWS.

Very sorry about that error
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
And you should not use 3 or 4 inside the for loops. It is < something.length.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
And if you have two ++ operators inside the same loop, you will run out of tokens when you are half-way through the array.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7032
    
  16

Karoline Lim wrote:But when the line 16 is unmasked, I got output like this,

Tip: avoid "combination code" like
System.out.println("Token value= " + tokens[index++]);
until you're a lot more confident with the language (and maybe not even then).

Use:
System.out.println("Token value= " + tokens[index]);
index++;


Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Karoline Lim
Greenhorn

Joined: Oct 14, 2011
Posts: 12
There is a big mistake. It shouldn’t be tokens.length / COLUMNS - ROWS. It should be tokens.length / COLUMNS / ROWS.

Very sorry about that error


It's ok, just a small mistake. Thanks alot! To make sure there's no 'fuzzyness', I hard-coded the for loop values to check the outcomes.

Really thanks Campbell, I've learned alot!

Tip: avoid "combination code" like
System.out.println("Token value= " + tokens[index++]);
until you're a lot more confident with the language (and maybe not even then).


It solves the problem, thank you so much Winston! No combination code!

Thanks again everyone.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
It’s not that it was combination code. It was that you have two ++ operators in the same loop.

But there are a lot of people who think you oughtn’t to use the ++/-- operators in combination.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
And you’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Int array to 3D array
 
Similar Threads
Flow Control.......
I am not getting the required output...
project euler
Output of this 2Dimensional array ?
ArrayIndex in Arrays