| Author |
Doubt regarding 2D String arrays in java
|
Mansukhdeep Thind
Ranch Hand
Joined: Jul 27, 2010
Posts: 1142
|
|
Hi
While trying out some mock tests, I cam across the following question:
My question is that how can we say ? When we are declaring and constructing a 2D array, aren't we supposed to put 2 square brackets on both sides?
|
~ Mansukh
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
It's confusing because you're thinking in terms of "2D arrays". Java doesn't have multidimensional arrays. String[][] means array of array of String.
If you assign a String[][] to an Object[], it means that each element of the object array will hold a String[].
|
 |
Mansukhdeep Thind
Ranch Hand
Joined: Jul 27, 2010
Posts: 1142
|
|
Thank you Stephen. Yes , you are correct, There are no 2D/3D/ND arrays in java, but only array or array.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4734
|
|
Stephan van Hulst wrote:If you assign a String[][] to an Object[], it means that each element of the object array will hold a String[]...
@Mansukhdeep: It's probably worth adding that you should almost never do this in practise.
In fact, the SCJP exam books are full of code that you would never use in real life; and my advice is to forget it as soon as you leave the exam room - you're unlikely to ever need them again, and they'll probably be more of a distraction than a help.
My advice: Write dumb code.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Mansukhdeep Thind
Ranch Hand
Joined: Jul 27, 2010
Posts: 1142
|
|
|
OK Winston. Perhaps it is as important to know what not to do as it is to know what is correct.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32604
|
|
Mansukhdeep Thind wrote: . . . There are no 2D/3D/ND arrays in java, but only array or array. 
Agree. But shouldn’t it read “array of array”?
|
 |
Mansukhdeep Thind
Ranch Hand
Joined: Jul 27, 2010
Posts: 1142
|
|
Aight!! Like Ali G says.. RESPECT main man!!
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Doubt regarding 2D String arrays in java
|
|
|