• 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

Array Declaration

 
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here shatner is declared as a multidimensinaol array with dimension [1][1]..
then how we can get shatner[0][3] in line number 7.
according to me we can only access shatner[0][0].
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek KumarSoni wrote:Here shatner is declared as a multidimensinaol array with dimension [1][1]..
then how we can get shatner[0][3] in line number 7.
according to me we can only access shatner[0][0].


It's because in Java there are no multidimensional arrays. There are merely array of arrays.
In line shatner[0] = rat you replaced old size-1 array with new size-4 array.
So now it is legal to access element shatner[0][3].
This is equivalent of:
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok pawel i got your point but are array of arrays and multidimensional array both different terms???
 
author
Posts: 23951
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

Abhishek KumarSoni wrote:ok pawel i got your point but are array of arrays and multidimensional array both different terms???




One of the differences of arrays of arrays versus a multidimensional array is that the array or arrays doesn't have to be a rectangle.

With a 2 dimensional array (provided by other languages, say C/C++), there are two indexes, say a row and column, and all the elements can be found with a row and column.

With arrays of arrays, you have an array with one index, say row, and each element in the array refers to another array. Each of these arrays, while also have an index, say column, can be of different sizes. For example, the first row may have four columns, while the second row may have only one column.... In your example, this is what happened after line 6.

Henry
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Visual explanation

This is an array of arrays:



And this is a multidimensional array:
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its ok.i got your point thanks a lot.
but could you please explain one more thing
int[] x, int y[];
here if [] sign is included before variable name then why any further defination of array add one more dimension like here i declare array y as a one dimensional array but it has become two dimensional on its own
Now if I

Here line number 3 will give you error reason given is that y is two dimensional array .
Could you please explain why such an unexpected behaviour?
 
Henry Wong
author
Posts: 23951
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

Abhishek KumarSoni wrote:
int[] x, int y[];
here if [] sign is included before variable name then why any further defination of array add one more dimension like here i declare array y as a one dimensional array but it has become two dimensional on its own
Now if I

Here line number 3 will give you error reason given is that y is two dimensional array .
Could you please explain why such an unexpected behaviour?




Basically, this ...



is the same as this set of declarations ...



which of course, is the same as this ...



Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic