Rajat Gupta
Rajat Gupta

Reputation: 26617

Storing multiple datatypes inside a single two dimensional array

I have a need to store multiple datatypes(like int or string mostly) inside a two dimensional array. Using Object[][] does solve the problem. But is it a good way to do so ??

How does the Object[][] array then reserve the heap space ? I mean, in accordance with which datatype? Does it leads to any wastage of resources ?

I was trying to do something like this:-

Object[][] dataToBeWritten={ {"Pami",34,45}, {"Ron","x",""}, {"spider","x",""} };

Edit: You may suggest any better alternatives also if there exists any..

Upvotes: 2

Views: 4761

Answers (4)

musiKk
musiKk

Reputation: 15189

Alternative:

Use proper classes. You are trying to take some dynamic approach in a statically typed language. The thing is that Object[] doesn't help the reader of your code one bit what he is reading about. In fact I can't even suggest a design for a class because I can't make sense of your example. What is {"Pami",34,45} and how is this supposed to be related to {"spider","x",""}?

So supposed this information is something foo-like you should create a class Foo and collect all that stuff in a Foo[] or a List<Foo>.

Remember: Not only comments store information about your code. The type system contains valuable information about what you're trying to accomplish. Object contains no such information.

Upvotes: 1

sfussenegger
sfussenegger

Reputation: 36115

See How to calculate the memory usage of a Java array and Memory usage of Java objects: general guide.

For example, let's consider a 10x10 int array. Firstly, the "outer" array has its 12-byte object header followed by space for the 10 elements. Those elements are object references to the 10 arrays making up the rows. That comes to 12+4*10=52 bytes, which must then be rounded up to the next multiple of 8, giving 56. Then, each of the 10 rows has its own 12-byte object header, 4*10=40 bytes for the actual row of ints, and again, 4 bytes of padding to bring the total for that row to a multiple of 8. So in total, that gives 11*56=616 bytes. That's a bit bigger than if you'd just counted on 10*10*4=400 bytes for the hundred "raw" ints themselves.

I think this is for Hotspot only though. References to any object are, just link ints, 4 byte each, regardless of the actual object, or the object being null. Spare requirement for the objects themselves is a whole different story though, as the space isn't reserved or anything the like at array creation.

Upvotes: 3

Sam Dufel
Sam Dufel

Reputation: 17608

An array of Objects is basically an array of pointers. However, that's what you get with any array of non-primitive types in Java - an array of Objects, and array of Strings, and an array of Robots of equal length take up the exact same amount of space. Heap space for the actual objects isn't allocated until you initialize the objects.

Upvotes: 1

Nick
Nick

Reputation: 25828

All objects are stored by reference. So a reference to the heap memory is stored. Therefore the amount of memory allocated for an array is one sizeof ( reference ) per entry.

Upvotes: 1

Related Questions