# Array of Nulls

posted 9 years ago

This isn't a homework question but for clearity I think I'll phrase it like one since I'm teaching this year...

Let n be an abitrary positive integer value (assume at least 100)

Let A be the memory allocated by the following single command:

Object[] x = new Object[n];

Let B be the memory allocated by the following single command:

double[] y = new double[n];

What is the approximate value of A/B? Or, alternatively which is bigger and by what percentage?

For my situation, I have a large n^k dimensional matrix that uses a ton of space and I'm trying to determine if there would like be space saved using either implementation.

[ September 13, 2006: Message edited by: Scott Selikoff ]

Let n be an abitrary positive integer value (assume at least 100)

Let A be the memory allocated by the following single command:

Object[] x = new Object[n];

Let B be the memory allocated by the following single command:

double[] y = new double[n];

What is the approximate value of A/B? Or, alternatively which is bigger and by what percentage?

For my situation, I have a large n^k dimensional matrix that uses a ton of space and I'm trying to determine if there would like be space saved using either implementation.

[ September 13, 2006: Message edited by: Scott Selikoff ]

[OCA 8 Book] [OCP 8 Book] [Blog] * SCJP (1.4, 1.6) * OCAJP 8 * OCPJP 8

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 9 years ago

Well, it's implementation-dependent, but in general new Object[n] should take 4*n bytes (each reference is a 32-bit value), and new double[n] should take 8*n bytes (each double being a 64-bit value). So looking just at the arrays, the double[] takes twice as much space. However the Object[] will probably need some actual Objects too (Doubles perhaps?), and depending on how many different objects you have, there's a good chance that the Object[] version will end up taking more total memory. Unless the references are mostly null or mostly to shared instances.

"I'm not back." - Bill Harding, *Twister*

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 9 years ago

Actually I am working with sparse matrixes, so perhaps I should rethink the construction. I thought using nulls (and then replacing with Double) would save space and it sounds like it would, but there are better ways to implement sparse matrices that I'm thinking I will try.

Thanks for the information though (I suspected 4*n but wasn't sure). Interestingly, if I used float[n] I'm guessing this would be the same as Object[n] plus the space for each object.

Thanks for the information though (I suspected 4*n but wasn't sure). Interestingly, if I used float[n] I'm guessing this would be the same as Object[n] plus the space for each object.

[OCA 8 Book] [OCP 8 Book] [Blog] * SCJP (1.4, 1.6) * OCAJP 8 * OCPJP 8

posted 9 years ago

As an aside, do you know of good sparse matrix implementions and/or one that used prefix matching techniques?

[OCA 8 Book] [OCP 8 Book] [Blog] * SCJP (1.4, 1.6) * OCAJP 8 * OCPJP 8

posted 9 years ago

Problem solved.... using arrays with null elements instead of a fully initialized one saved insane amounts of space.

Part of the reason I was having trouble is because I'm creating a dynamic matrix on the fly using reflection so I had to modify the reflection classes used to construct the array (so don't construct nxnxn, construct nx3, and put [][] pointers in the columns initially set to null).

Part of the reason I was having trouble is because I'm creating a dynamic matrix on the fly using reflection so I had to modify the reflection classes used to construct the array (so don't construct nxnxn, construct nx3, and put [][] pointers in the columns initially set to null).

[OCA 8 Book] [OCP 8 Book] [Blog] * SCJP (1.4, 1.6) * OCAJP 8 * OCPJP 8

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 9 years ago

Never used it myself, but I heard good things about Colt back when it came out, years ago. (Not about sparse matrices in particular, but the library in general.) Haven't heard much since - I haven't done much resembling real math in some time. No idea about prefix matching techniques in matrices, sorry. Though it sounds like you're in good shape now anyway...

**[Scott]: As an aside, do you know of good sparse matrix implementions and/or one that used prefix matching techniques?**

Never used it myself, but I heard good things about Colt back when it came out, years ago. (Not about sparse matrices in particular, but the library in general.) Haven't heard much since - I haven't done much resembling real math in some time. No idea about prefix matching techniques in matrices, sorry. Though it sounds like you're in good shape now anyway...

"I'm not back." - Bill Harding, *Twister*