• 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

multidimensional array doubt

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please consider the given scenario:

int[][] k = new int[3][4];
k[0] ={1,2,3,4}; // gives compiler error

whereas

int[] g = {1,2,3,4}; // works fine.



in the above case k[0] is also an array reference variable of type int which can refer to array object so it should work fine like the second case. put in other words "g" is array reference of type int and k[0] is also array reference of type int so they should work alike? please explain ?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The shortcut syntax to initialize an array using braces { and } is valid only when you declare the array.

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:please consider the given scenario:

int[][] k = new int[3][4];
k[0] ={1,2,3,4}; // gives compiler error

whereas

int[] g = {1,2,3,4}; // works fine.



in the above case k[0] is also an array reference variable of type int which can refer to array object so it should work fine like the second case. put in other words "g" is array reference of type int and k[0] is also array reference of type int so they should work alike? please explain ?



k[0] ={1,2,3,4}; you are assigning elements in a reference variable that's the problem ,while in int[] g = {1,2,3,4}; here you are assigning elements in array.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic