• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Int array to 3D array

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've found out the reason already.
 
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you should not use 3 or 4 inside the for loops. It is < something.length.
 
Campbell Ritchie
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74348
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you’re welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic