Array
Moderator: admin
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Array
Q:-
public static void main(String[] args){
int i=4;
int ia[][][] = new int[0];
System.out.println( ia.length + ", " );
System.out.println( ia[0].length);
System.out.println( ia[0][0].length);
}
}
answer is :-
4,
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Mock.main(Mock.java:9)
why not 4,0,4
public static void main(String[] args){
int i=4;
int ia[][][] = new int[0];
System.out.println( ia.length + ", " );
System.out.println( ia[0].length);
System.out.println( ia[0][0].length);
}
}
answer is :-
4,
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Mock.main(Mock.java:9)
why not 4,0,4
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Array
Why do you think it should be 4 0 4?
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Re: Array
because value of i=4
and its ia,ia[0],ia
so its 4,0,4
as when i tried ia,ia,ia[o]
i got- 4,4,0.
and its ia,ia[0],ia
so its 4,0,4
as when i tried ia,ia,ia[o]
i got- 4,4,0.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Array
You are creating a three dimensional array. The size of the first dimension is 4. So ia refers to an array of length 4. Thus, System.out.println( ia.length + ", " ); prints 4.
Each element of this array itself points to a two dimensional array. But you have given the size of each of these two dimensional arrays as 0. i.e. ia[0] basically points to a two dimensional array of length zero. Therefore, System.out.println( ia[0].length); prints 0.
Now, when you try to access ia[0][0], what do you expect ? Is there a 0th element in ia[0]?
Each element of this array itself points to a two dimensional array. But you have given the size of each of these two dimensional arrays as 0. i.e. ia[0] basically points to a two dimensional array of length zero. Therefore, System.out.println( ia[0].length); prints 0.
Now, when you try to access ia[0][0], what do you expect ? Is there a 0th element in ia[0]?
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Re: Array
I guess ia[0][0] will have have a 0 element as ia[0] doesn't have any other element.
but i am still confused why ArrayIndexOutOfBoundsException was thrown?
but i am still confused why ArrayIndexOutOfBoundsException was thrown?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Array
1. What is the index of the first element in an array?
2. Have you read the JavaDoc API description of ArrayIndexOutOfBoundsException ? That will tell you why is it thrown.
2. Have you read the JavaDoc API description of ArrayIndexOutOfBoundsException ? That will tell you why is it thrown.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Re: Array
zero.
sure will look into .
sure will look into .
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Array
Good, so what will arrayRef[0] point to if the size of an array is zero?ashishrai.kv wrote:zero.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Re: Array
nothing, as the arrayref size is 0 so it will show the ArrayIndexOutOfBound error, if i am not wrong
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Array
Well, there is your answer then
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Tue Jan 09, 2018 2:12 am
- Contact:
Re: Array
thanks
Who is online
Users browsing this forum: Bing [Bot] and 113 guests