Re: how to solve a special JRuby and Java syntax conflict? - correction to my 'final' summary?


I hope this won’t be taken the wrong way:
I’m rather glad you had the problem and posted your question,
because it prompted me to actually try out what I’d intended to do,
and even more because Carl Leiby-2 gave a very elegant way of
converting a whole array which I probably would not have found myself,
and which will save me writing quite a few lines of code
using the way I originally posted.

So, to summarise for my benefit:
in JRuby you can create a two dimension Ruby Array of doubles,
and then convert it in one go to a JRuby Java Array as follows:
rarr = [ [0.0, 0.1], [1.0, 1.1, 1.2, 1.3], [2.0], [3.0, 3.1, 3.2] ]
jarr = rarr.to_java Java::double[]
Note that it works even with different sizes of “sub-arrays”.
(But it won’t work if - for example - you replace the [2.0] by 2.0.)
For access to individual elements of the JRuby Java Array you can do:
puts jarr[2][0] # getter
jarr[2][0] = 2.0002 # setter
puts jarr[2][0] # getter
and - obviously - can write JRuby methods which process multiple
by wrapping the access to individual elements.
So, if, for some reason, the “to_java Java::double[]” way doesn’t work,
we can call a user-written Java method to create a Java Array
with the appropriate dimensions and with values 0.0,
and then populate it from JRuby by accessing the individual elements,
or by passing individual values and their “indexes” to a user-written
Java method which then sets the appropriate element in the Java Array.
(I’m happier when I have more than one way to do things, as a

For a one dimensional array the Java conversion is as follows:
rarr = [0.0, 1.1, 2.2]
jarr = rarr.to_java Java::double
with individual access - as you’d expect - like: jarr[2] = 2.0002

For a three dimensional array the Java conversion is as follows:
rarr= [ [ [0.001, 0.01], [0.101, 0.11, 0.12] ], # [0][0][i] &
[ [ 1.001] ] # [1][0][i]
jarr = rarr.to_java Java::double[][]
with individual access - as you’d expect - like: jarr[1][0][0] = 1.01001

And - presumably - so on for more dimensions.

So thanks for the original post, and for posting in the JRuby forums!