• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Multi-dimension arrays

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a multi-dimensional array that I want to loop through, how do I setup the for loop? I thought this would work, but it doesn't compile.




I know I can do it with a regular but I was wondering if there was a way to do it as above.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the first hierarchy you loop over arrays of objects (not object). Take a look:

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
better to say *array of arrays* insteadof multidimensional . your for loop declaration is wrong .
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, let's say 'array of arrays' ... but because arrays are objects too (special ones of corse), erm ... nevermind ; - )
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:On the first hierarchy you loop over arrays of objects (not object). Take a look:



Ok, I get it. In the outer loop "r" actually becomes an array for the row, then in the inner loop the "c" becomes the individual cell within the row. Out of curiosity, if there are three dimensions to the array, would this be the code:

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:better to say *array of arrays* insteadof multidimensional . your for loop declaration is wrong .




Sorry, multi-dimensional is how I've always seen nested arrays referred to. I'm guessing "array of arrays" is a java term. I'll try to remember this in the future.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Lipay wrote:Ok, I get it. In the outer loop "r" actually becomes an array for the row, then in the inner loop the "c" becomes the individual cell within the row. Out of curiosity, if there are three dimensions to the array, would this be the code:



Not exactly. I'd say it has to look like this:
 
Marshal
Posts: 79653
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java arrays are not multi-dimensional but arrays of arrays. If they were multi-dimensional, the individual members would have to be the same size, which is not the case in Java. Arrays of arrays may also allow different placement of the data in memory, re-assignment of members, etc.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:

Mike Lipay wrote:Ok, I get it. In the outer loop "r" actually becomes an array for the row, then in the inner loop the "c" becomes the individual cell within the row. Out of curiosity, if there are three dimensions to the array, would this be the code:



Not exactly. I'd say it has to look like this:



with the multidimensionality of the array of arrays ... i guess i am quoting write

the outermost for loop argument contains the most significant array of the arrays and with each inner loop argument containing the less significant one than the previous one

Lucky
 
I AM MIGHTY! Especially when I hold this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic