Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Two Dimensional Arrays

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try 5 values or 6 values.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Aruna Balasuriya
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic