We can easily create 2,3,4 dimensional array..but i want to know how to create very large dimensional array
-
5The same way you'd create a 2, 3, or 4 dimensional array. Just... a lot less readable. (I can't help but wonder if the data you're representing could be better suited to a class or set of classes.)David– David2012-07-04 14:36:54 +00:00Commented Jul 4, 2012 at 14:36
-
Agreed. I can't think of a readable way to traverse through a 400 dimensional array.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz– zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz2012-07-04 14:38:08 +00:00Commented Jul 4, 2012 at 14:38
-
4Keep typing....just more square brackets. Sounds pretty ridiculous to me.duffymo– duffymo2012-07-04 14:38:28 +00:00Commented Jul 4, 2012 at 14:38
-
1Humor me -- why the hell would you need something like that? Can't you make your own classes? I would really enjoy seeing you iterate through the 300th. :Dcarlspring– carlspring2012-07-04 14:40:40 +00:00Commented Jul 4, 2012 at 14:40
-
5Reality check: A 400 dimension array with a length of 2 each would require 10^122 bytes of memory which is not supportted by any 64-bit machine or even a machine with 128-bits or 256-bits of address space.Peter Lawrey– Peter Lawrey2012-07-04 15:03:56 +00:00Commented Jul 4, 2012 at 15:03
4 Answers
You could use Array.newInstance, but:
The number of dimensions of the new array must not exceed the number of array dimensions supported by the implementation (typically 255).
But I believe it would be better for you to create better abstractions for your domain. Dealing with such an array would be difficult. Maybe you can give more information about what you're trying to achieve so that a better answer can be provided.
1 Comment
The simple answer is by typing lots of []'s. (But the practical limit is 255. This is imposed by the JVM specification (in Section 4.3.2) rather than the JLS; see Maximum number of dimensions in a Java array)
But you really wouldn't want to do this:
Your code will be horrible to read and maintain. Each time you need to fetch an element you have to supply 400 index expressions.
A 400 dimensional array that was represented this way would take a ridiculous amount of memory ...
If you are serious about this, you should look into some kind of sparse representation. But even then, conventional array computations are hardly going to be practical.
Comments
Creating a 400 dimensional array would be harder to maintain/read in the long run and would also waste a lot of memory for you. I would suggest that you create a class to store your data and start adding the objects of this class to any of the implementation of the java.util.List interface.
This way you're storing your data in a much more readable format as well.