I was thinking about creating a List of Lists of Lists, but is this really the best way of doing this in Java? Would this create a lot of memory overhead for a huge 3D-array?
The best structure here will depend heavily on how it is likely to be used.
Firstly, if the dimensions of your array reflect different types (e.g. an array of demographic data with age, income and region as dimensions) then it's a good idea to encapsulate each of these as an enum or class rather than try to encode in an int just for the purpose of indexing in an array. Things like EnumMap are very efficient in storage and access.
If it will be a sparse array (i.e. will have many null or empty values) then your best structure may be a Map from coordinates to value. In that case the size of the dimensions is irrelevant: the number of values is the only important thing.
On the other hand if the array has fixed size with all cells having a value then a traditional [ ][ ][ ] array could well be your most efficient mechanism.
However ultimately your design should naturally reflect your problem domain: it's usually a mistake to attempt to optimise for speed or memory early on. Solve those problems later when you know they really are problems. And when you do solve them make sure the optimised implementation details are hidden behind a well defined interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With