What is the most efficient way to convert the following data structure from C to Java?
struct {
int a; int b; int c; int d;
double e;
} foo[] = {
{ 0, 1, 2, 42, 37.972},
/* ... 100 more struct elements ... */,
{ 0, 3, -1, -4, -7.847}
}
I best I could think of is this:
class Foo {
public int a;
public int b;
public int c;
public int d;
public double e;
public Foo(int a, int b, int c, int d, double e) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
}
}
Foo[] foo = new Foo[] {
new Foo(0, 1, 2, 42, 37.972),
/* ... 100 more objects ... */
new Foo(0, 3, -1, -4, -7.847)
}
But it uses way too many objects for holding simple primitives.
No, it doesn’t. A Java
intordoublefield is not an object; it will be allocated on the heap as part of its containingFooinstance.So in your case there will be 101 objects: one
Foo[]array, and 100Fooinstances.Or do you want all of that to be a single “flat” array? Sorry, no can do in Java, that’s not how it works.
Then again, what exactly are you worrying about here? 101 Objects of this kind are nothing, we’re talking about less than 4KB here!