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

posted 5 years ago

1

I've found out the reason already.

Campbell Ritchie

Sheriff

Posts: 55371

157

posted 5 years ago

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

posted 5 years ago

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)