aspose file tools*
The moose likes Java in General and the fly likes Two Dimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Two Dimensional Arrays" Watch "Two Dimensional Arrays" New topic
Author

Two Dimensional Arrays

Aruna Balasuriya
Ranch Hand

Joined: Nov 14, 2009
Posts: 44
public class Test {

public static void main(String[] args) {

int x [] [] = new int [3][5] ;
x[0]=new int[]{1,2,3,4,5,6,7} ;

}

}

According to declaration x[0] may contain only 5 values but I fill 7 values but it wont generate array out of bound exception.why?
Addy Micheal
Greenhorn

Joined: Nov 17, 2009
Posts: 1
try 5 values or 6 values.


Solar Panels Australia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Aruna Balasuriya wrote:
According to declaration x[0] may contain only 5 values but I fill 7 values but it wont generate array out of bound exception.why?


Keep in mind that Java doesn't really support multidimensional arrays. It supports arrays of arrays. When you instantiated the two dimensional array, the new operator instantiates an array of size three, and then instantiates int arrays of size 5, in each of the three array elements.

In the next statement, you are instantiating an int array of size seven, and replaced the first element of the int array array with it. So, X is an array of arrays, with the first element having an int array of size 7, and the other two elements having an int array of size 5.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aruna Balasuriya
Ranch Hand

Joined: Nov 14, 2009
Posts: 44
Aruna Balasuriya wrote:public class Test {

public static void main(String[] args) {

int x [] [] = new int [3][5] ;
x[0]=new int[]{1,2,3,4,5,6,7} ;

}

}

According to declaration x[0] may contain only 5 values but I fill 7 values but it wont generate array out of bound exception.why?


Trying with 6 vales also DIDNOT result array out of bound exception.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Aruna Balasuriya wrote:
Trying with 6 vales also DIDNOT result array out of bound exception.


For the same reason, that I explained in my previous post.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two Dimensional Arrays