This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Question on 2D Arrays  RSS feed

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output of the Above Code is

1 2 3 4
3 4
5 6 4 5 4 3 6 7 8

If I try to add one more row element using statement "twoD[3] = new int[]{33,34};" , it throws ArrayIndexOutOfBoundsException. It makes sense because array is initialized to have only 3 rows . But it is allowing me to add more columns .As you can see in above example ,though array twoD is initialized to have 2 columns ,it is letting me to add more than 2 columns in some of row elements. Why adding columns is not throwing ArrayIndexOutOfBoundsException ? . Does this mean column's capacity can be increased although it is initialized to 2 ?

Thanks,
Veena
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What this setup means, that you are given an array, which can hold another 3 arrays, where each of those can hold 2 elements, and so it looks like:
{
 {0, 0},
 {0, 0},
 {0, 0}
}


An array is a special type object. So when you say twoD[0] = new int[]{1,2,3,4};, you say replace array's elemnt 0 (which itsel is an array) with another array, which in turn is:
{
 {1, 2, 3, 4},
 {0, 0},
 {0, 0}
}


With the rest is same.
 
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Veena Pointi wrote:

Output of the Above Code is

1 2 3 4
3 4
5 6 4 5 4 3 6 7 8

If I try to add one more row element using statement "twoD[3] = new int[]{33,34};" , it throws ArrayIndexOutOfBoundsException. It makes sense because array is initialized to have only 3 rows . But it is allowing me to add more columns .As you can see in above example ,though array twoD is initialized to have 2 columns ,it is letting me to add more than 2 columns in some of row elements. Why adding columns is not throwing ArrayIndexOutOfBoundsException ? . Does this mean column's capacity can be increased although it is initialized to 2 ?

Thanks,
Veena

array is an object in Java that means it is fixed once you create its object you cannot modify it array size so you had created array of size 3 it can hold 3 rows only when you try to add row it throws exception
 
Veena Pointi
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for replying.You said "where each of array can hold 2 elements". But first row I am able to add 4 elements to first array . So my question is why is it letting me add 4 elements when I have specified 2 . Why is it not throwing ArrayIndexOutOfBounds exception ?
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, please don't quote an entire reply, especially if it's long. Rather, quote only relevant parts that you're responding to. If you can't pick out a relevant part then prefer not to quote at all rather than quoting everything.

Thanks for your cooperation.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Veena Pointi wrote:So my question is why is it letting me add 4 elements when I have specified 2 . Why is it not throwing ArrayIndexOutOfBounds exception ?


You're not actually adding columns. Rather, you're replacing the 2-element array with a new 4-element array.

Java does not have multi-dimensional arrays, it has nested arrays. When you initialize the array with new int[3][2] the [2] part only serves to define the sizes of the nested arrays. If you replace those initial arrays with larger ones that is perfectly legal but don't think of it as "expanding" the original arrays you had since that is not what is happening at all.

If you want to have more elements in the outer array (the one that holds all the "rows") then you'll have to replace the entire nested array with a new one.
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, line 1 does not actually need the [2] part. It will work the same if you just wrote new int[3][] instead.
 
Veena Pointi
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu . Multi dimensional arrays referred as "Nested Arrays" made it more easier to understand.Basically we don't have to specify size for nested arrays.Even if we do , size is ignored for nested arrays. I tested couple of examples . Thank you for your clear explanation .
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Veena Pointi wrote:Even if we do , size is ignored for nested arrays.


Not quite true. If you specify a size for the nested arrays, Java will initialize them to arrays of that size, just as Liu illustrated.

JShell actually gives some good visualization of this as well:

jshell> int[][] nums = new int[3][2]
nums ==> int[3][] { int[2] { 0, 0 }, int[2] { 0, 0 }, int[2] { 0, 0 } }
                    ^-- nums[0] --^  ^-- nums[1] --^  ^-- nums[2] --^

Whereas if you do this:

jshell> nums = new int[3][]
nums ==> int[3][] { null, null, null }


then the nested elements are initialized to null.

To "resize" the outer array, you have to create a whole new array:

jshell> nums = new int[4][]
nums ==> int[4][] { null, null, null, null }


And if you want the nested arrays to be initialized, then you do this:

jshell> nums = new int[4][3]
nums ==> int[4][] { int[3] { 0, 0, 0 }, int[3] { 0, 0, 0 } ...  0 }, int[3] { 0, 0, 0 } }



 
Veena Pointi
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice ,clear and in depth explanation . Thank you for taking time to explain .
 
Hold that thought. Tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!