File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding 2D String arrays in java

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephen. Yes , you are correct, There are no 2D/3D/ND arrays in java, but only array or array.
 
Winston Gutkowski
Bartender
Pie
Posts: 9461
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Winston. Perhaps it is as important to know what not to do as it is to know what is correct.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aight!! Like Ali G says.. RESPECT main man!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic