Java - Array Declaration


Array DeclarationWhat is an Array:-


An array is an index collection of fixed number of homogeneous data elements.

The main advantage of an array is we can represent a huge number of elements by a single variable.

But the main disadvantage of an array is it is fixed in size so that once the array is created then there is no chance of increasing the size of the array based on our requirement.

Hence to use an array, we should know the size in advance.



How to declare an array?

Let's see in how many ways we can declare an array.


1-D Array Declaration:-


int [ ]    x;       (Recommended to use)


int    [ ]x;


int      x[ ];


 Above all three declaration of the 1-D array is valid.If we are using other than this then we will get compile time error.


 


2-D Array Declaration:-


 int [ ][ ]     x;


 int          [ ][ ]x;


 int         x[ ][ ];


 int  [ ]   x [ ];


int [ ]        [ ]x;


int       [ ]x [ ];


Above all declaration of 2-D array is valid.If we are using other than this than we will get compile time error.

 

3-D Array Declaration:-


int  [ ][ ][ ]      x;

int       [ ][ ][ ]   x;


int      x[ ][ ][ ]  ;


int [ ] x[ ][ ]     ;


int [ ]      [ ][ ]x;


int [ ]      [ ] x[ ] ;


 int  [ ][ ]          [ ]x;


int  [ ][ ]      x[ ];


int [ ][ ]x[ ];


int          [ ]x [ ][ ];


Above all declaration of 3-D array is valid. If we are using other than this than we will get compile time error.


No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com