This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Array dimension - pl explain

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is regarding an array
type1:
int[][] i = {{1,2,3,4},{4,5,6},{1,2,3}};

Can I declare like above ? Usually it should be like below right ?
type:2
int[][][] i = {{1,2,3,4},{4,5,6},{1,2,3}};
Note: see an aditional bracket on left

Both are getting compiled.
But in Type 1 left side (Declaration side we have two square brackets) and right side we have 3 set of values. In this case what is the array dimension ? Can anybody explain the difference ? thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure they both compile?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[][][] i = {{{1,2,3,4},{4,5,6},{1,2,3}}};

If this is the array, then length means how many two dimensional arrays are present in this array.
Ans is 1.

int[][] i = {{1,2,3,4},{4,5,6},{1,2,3}};

For this, length means how many one dimensional arrays are present and it is 3.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
type1:
int[][] i = {{1,2,3,4},{4,5,6},{1,2,3}};

This type is no problem, and will compiled.

int[][][] i = {{1,2,3,4},{4,5,6},{1,2,3}};
Note: see an aditional bracket on

This one will not compiled, they are just incompatible type, the left side require a 3-Dim array, but the right side is just a 2-DIM array.
 
reply
    Bookmark Topic Watch Topic
  • New Topic