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

Question about multidimensional arrays

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

Another question from the K&B bonus master exam from the attached CD. A question was asked about many multi-dimensional arrays. Say you have the followig array declared:



And now we are going to store a new 2 dimensional array into that array:



I didn't expect this to work (maybe at compile time, but certainly not at runtime). What happens in memory?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim Pouwels,

In Java, there's nothing like 'multi dimensional array'. There are simply arrays. Those can contain simple objects, or arrays. Those arrays can further contain simple objects or arrays and so on.

So, when we say something like

we are actually declaring an array arr2 of length 1, which will contain an array of length 1, which will contain 'int' data.

Now, coming back to your question, when we say

we say following:
declare an array of length 1, which will store an array of length 1, which will store an array of length 1, which will store an array of length 1, which will store int data.

After that, now we know that:
1) arr4[0][0][0][0] is int
2) arr4[0][0][0] is an array which stores int data
3) arr4[0][0] is an array which stores an array which stores int data

So, we can assign any 'array which stores an array which stores an array' to arr4[0][0]. This new data not necessarily should have same length.
e.g. we can have:

In same way, we are having

So, now, arr4 is - an array of length 1 which contains an array of length 1 which contains an array of length 100 which contains an array of length 100 which contains int data.

To be frank, unlike C/C++ in Java, we don't care about array's memory layouts (because we cannot increase reference address by n*sizeof(data) to access 'n'th element)

I hope this helps.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx! This clearifies it!
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic