All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Array
PostPosted: Tue Feb 13, 2018 10:09 am 
Offline

Joined: Tue Jan 09, 2018 2:12 am
Posts: 10
Q:-
public static void main(String[] args){
int i=4;
int ia[][][] = new int[i][0][i];
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


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Tue Feb 13, 2018 12:22 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6980
Why do you think it should be 4 0 4?


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Tue Feb 13, 2018 1:12 pm 
Offline

Joined: Tue Jan 09, 2018 2:12 am
Posts: 10
because value of i=4
and its ia[i],ia[0],ia[i]
so its 4,0,4

as when i tried ia[i],ia[i],ia[o]

i got- 4,4,0.


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Tue Feb 13, 2018 9:55 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6980
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]?


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Wed Feb 14, 2018 3:57 am 
Offline

Joined: Tue Jan 09, 2018 2:12 am
Posts: 10
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?


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Wed Feb 14, 2018 5:52 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6980
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.


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Wed Feb 14, 2018 7:48 am 
Offline

Joined: Tue Jan 09, 2018 2:12 am
Posts: 10
zero.

sure will look into . :D :D


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Wed Feb 14, 2018 8:25 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6980
ashishrai.kv wrote:
zero.

Good, so what will arrayRef[0] point to if the size of an array is zero?


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Wed Feb 14, 2018 8:46 am 
Offline

Joined: Tue Jan 09, 2018 2:12 am
Posts: 10
nothing, as the arrayref size is 0 so it will show the ArrayIndexOutOfBound error, if i am not wrong


Top
 Profile Send private message  
 
 Post subject: Re: Array
PostPosted: Wed Feb 14, 2018 8:58 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6980
Well, there is your answer then :D


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum