1

We can easily create 2,3,4 dimensional array..but i want to know how to create very large dimensional array

7
  • 5
    The 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.) Commented Jul 4, 2012 at 14:36
  • Agreed. I can't think of a readable way to traverse through a 400 dimensional array. Commented Jul 4, 2012 at 14:38
  • 4
    Keep typing....just more square brackets. Sounds pretty ridiculous to me. Commented Jul 4, 2012 at 14:38
  • 1
    Humor 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. :D Commented Jul 4, 2012 at 14:40
  • 5
    Reality 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. Commented Jul 4, 2012 at 15:03

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You can't have more than 255 dimensions in Java, nor would you want to.
4

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

2

What you can do is use a BigInteger to encode the 400 dimensions as an integer and use this as a key to a HashMap or TreeMap. Your array needs to be very, very, very sparse or you will quickly reach your memory limit. Even 400 dimensions of size 2 is 2.6e120

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.