File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring a 2D array

 
Charles Mara
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I know you can declare a 1D array this way


is there a away to do this for a 2D array?
 
Charles Mara
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind a deeper google got me the answer
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you found you can't create a 2-D array in java, just plain old 1-D ones that can hold anything...including arrays?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually your original code has some poor style. You should but the [] earlier.
Needless to say, this sort of question comes up frequently, and here is some good discussion of it. But as Fred has already hinted, you are not creating a 2D array.
 
Charles Mara
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replys.

So I should declare arrays this way

is that right?

and no I didnt relise that arrays could hold other arrays but I do now!Will look in to it further just to make sure I have the understanding of them correct.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should declare arrays like that, [] before the name of the array. That is because the variable is of type something-array, so you declare the type as something-array, then the name.
int[][] arr is not a 2D array, but a 1D array containing however many 1D arrays. Or more precisely, enough memory put aside to contain that in the reference. arr[123] is a 1D array, provided arr has been instantiated with enough elements to support an index of 123.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic