• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Int array to 3D array

 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found out the reason already.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you should not use 3 or 4 inside the for loops. It is < something.length.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karoline Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic