0

Just trying to declare and initialize a 2D array using the codemodel library by Sun but I'm having some issues. I've tried:

JBlock.decl(model.LONG.array().array(), "arrayName", JExpr.newArray(model.LONG, n));

which outputs:

long[][] arrayName = new long[ n ] ;

But I can't seem to find a way to fill in the last [ ]. I've tried several variations.

To be more specific, I need to be able to initialize with something like:

(int) Math.sqrt(array.length);

JExpr.ref() doesn't seem to work for this.

Any help would be appreciated. Thanks

s990x
  • 1
  • 2

2 Answers2

0

how about using

JExpr.newArray(model.LONG.array(), n)

as the last argument.

kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • this creates the new [], thanks. going to experiment with it. – s990x Nov 29 '13 at 00:10
  • what does it output ? – kiruwka Nov 29 '13 at 00:10
  • it outputs new long[n][]; unsure how to fill in the dimension of the inner arrays – s990x Nov 29 '13 at 00:18
  • well, i actually thought that is what you need from your question. Note that `long[][] arrayName = new long[n][]` is a valid java array declaration and compiles fine. It defines `n-sized` array of long[]. Each of the element of this array can be declared later, and could, in fact be different length, for example : arrayName[0] = new long[3]; arrayName[1] = new long[1]; // another size. But probably you know that already – kiruwka Nov 29 '13 at 00:23
  • initializing them later looks a little messy. was hoping to save the code by being able to add in that one digit. im new to this so maybe theres something im missing. – s990x Nov 29 '13 at 02:47
0
JBlock.decl(model.LONG.array().array(), "arrayName", JExpr.ref("[n][n]"));
Stevantti
  • 494
  • 2
  • 6
  • 13
  • this doesnt work for more complicated entries like:(int) Math.sqrt(array.length) – s990x Nov 29 '13 at 02:43
  • @s990x well, it will work if you convert it to string first and then create your argument to `JExpr.ref` – kiruwka Nov 29 '13 at 14:37
  • any idea how to initialise the same. what i mean is https://stackoverflow.com/questions/55790450/how-to-initialise-a-2d-array-using-codemodel – Monis Majeed Apr 22 '19 at 09:03