Array Shortcut Method
1.For 1-D Array:-
We can declare, create & initialize an array into a single line.
This is the short cut method what we are discussing here.
let's go...example :
int [ ] x= new int [3];
x[0]=10;
x[1]=20;
x[2]=30;
we can do this into a single line as follows:-
int [ ] x={10,20,30}; (perfectly valid)
But if we want to use this shortcut compulsory we should perform all activities into a single line only.
if we are trying to do in multiple lines then we will get Compile-Time Error.
example:-
int [ ] x ; Declaration is valid
x={10,20,30} But Compile-Time Error : Illegal start of Expression
2.For 2-D Array:-
we can use this shortcut method even for multidimensional array also.
int [ ] [ ] x={{10,20},{30,40,50}}; (perfectly valid)
3. For 3-D Array
example:
int [ ] [ ] [ ] x = int {{{10,20,30},{40,50,60}},{{70,80},{90,100,110}}};
Try to find out the outputs:-
System.out.println(x[0][1][2]);
System.out.println(x[1][0][1]);
System.out.println(x[2][0][0]);
System.out.println(x[1][2][0]);
System.out.println(x[1][1][1]);
Now scroll down for answers:
System.out.println(x[0][1][2]); o/p is 60
System.out.println(x[1][0][1]); o/p is 80
System.out.println(x[2][0][0]); Runtime Exception :ArrayIndesxBoundException
System.out.println(x[1][2][0]); Runtime Exception :ArrayIndesxBoundException
System.out.println(x[1][1][1]); o/p is 100
We can declare, create & initialize an array into a single line.
This is the short cut method what we are discussing here.
let's go...example :
int [ ] x= new int [3];
x[0]=10;
x[1]=20;
x[2]=30;
we can do this into a single line as follows:-
int [ ] x={10,20,30}; (perfectly valid)
But if we want to use this shortcut compulsory we should perform all activities into a single line only.
if we are trying to do in multiple lines then we will get Compile-Time Error.
example:-
int [ ] x ; Declaration is valid
x={10,20,30} But Compile-Time Error : Illegal start of Expression
2.For 2-D Array:-
we can use this shortcut method even for multidimensional array also.
int [ ] [ ] x={{10,20},{30,40,50}}; (perfectly valid)
3. For 3-D Array
example:
int [ ] [ ] [ ] x = int {{{10,20,30},{40,50,60}},{{70,80},{90,100,110}}};
Try to find out the outputs:-
System.out.println(x[0][1][2]);
System.out.println(x[1][0][1]);
System.out.println(x[2][0][0]);
System.out.println(x[1][2][0]);
System.out.println(x[1][1][1]);
Now scroll down for answers:
System.out.println(x[0][1][2]); o/p is 60
System.out.println(x[1][0][1]); o/p is 80
System.out.println(x[2][0][0]); Runtime Exception :ArrayIndesxBoundException
System.out.println(x[1][2][0]); Runtime Exception :ArrayIndesxBoundException
System.out.println(x[1][1][1]); o/p is 100
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