Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Doubt regarding 2D String arrays in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding 2D String arrays in java" Watch "Doubt regarding 2D String arrays in java" New topic
Author

Doubt regarding 2D String arrays in java

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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: 4087
    
  18

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[].


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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: 8661
    
  23

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?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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: 44483
    
  34
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: 1157

Aight!! Like Ali G says.. RESPECT main man!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding 2D String arrays in java
 
It's not a secret anymore!