• 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

Proving 2-d array stores references

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen (and experienced in my youth, which was only a few weeks ago I think) some confusion about the true nature of two-dimensional arrays in Java. I hope this explanation by example helps clear some of the fog for newbies.
A two dimensional array consists of a one dimensional array of references to one-dimensional arrays. The following code uses standard Java reference equality tests and reference reassignment to prove this.
First, we create a two-dimensional array of primitive int values. The program then prints the length, and calls a method for printing the array contents. If the two-dimensional array, intArray, were on object containing all elements listed in the initialization, the program would print "intArray has 12 elements". But, it prints "intArray has 3 elements", which corresponds to the number of inner braces in the initialization.
Calling printArray(), we see each array, its length, and its contents. If intArray were one object, it's elements intArray[0], intArray[1] and intArray[2] would not have individual length attributes.
Next, we create a one-dimensional int array, int2, and assign it to intArray[2]. Calling printArray() now shows that intArray[2] has been replaced by the new int2 array. Only another reference to an array object, like intArray[2], would have been validly assigned to the array referenced by int2.
For our final proof, we have the reference equality test, which proves that intArray[2] and int2 point to the same object because their references are equal.

I hope this helps clarify how multidimensional arrays work. If I put anybody to sleep with this, though, then maybe I'll just bottle it as a cure for insomnia!
Regards,
Jeff
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This recent thread is somewhat relevant.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jason.
Regards,
Jeff
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic