• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

multidimensional array confusion

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I need help understanding multidimensonal arrays. I get lost after 2 dimensions.
for example:
int [] [] scores = int[2][3]

I understand this means I have a 2D array object referenced by scores [2][], which is 2 rows long.
Inside each of the 2 rows is a 1D array object -3 columns longs each, referenced by scores [0] and scores[1].

But how do you explain:
int [][][] scores = int[2][3][3]
OR
int [][][][] scores = int[2][3][3][3]

Please excuse me if I my terminology or examples are incorrect.
I read the section in K&B SCJP6 chapter 3 about multidimesional arrays, but I just don't quite get it.

Please help
-Thanks in advance
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to think about arrays in Java, is Java does *not* support multidimensions arrays. Multidimensional arrays are just arrays of arrays.

So...

int scores[][] = new int[2][3];

Is just an array of array of ints. The first dereferences will dereference the array of array of ints to just an array of ints... meaning scores[1] is an array of ints.... and scores[1][1] will derefence the array of int to just an int.

Henry
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To continue...

int scores[][][][] = new int[2][3][4][3];

Here scores is an array of array of array of array of ints. If you dereference it twice...

scores[1][2]

this represents an array of array of ints.

So...

int oldscores[][] = scores[1][2];

Since scores is an array of array of array of array of ints, dereferencing it twice will yield an array of array of ints, which of course, can be assigned to an array of array of ints.

Henry
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java may not support multidimensional arrays, but its certainly on the test.
I am having difficulty visualizing a 3D array so I can answer the questions.
I'll post the specific question I'm having difficulty with
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic