Convert Ruby Array to Java Array

Hi everyone,

Recently I would like to convert Ruby array to Java array with ‘double’
elements. I use syntax:

ruby_array = [[1,2],[3,4]]
java_array = ruby_array.to_java :Double

which return: “java.lang.reflect.Array:-2:in `set’:
java.lang.IllegalArgumentException: array element type mismatch…”

When I use: “java_array = ruby_array.to_java :String”, it works fine.

Can anyone help me of that problem?

I use: JRuby 1.1.4, Windows XP

Thanks a lot :slight_smile:

Marcin

Marcin Piórkowski wrote:

When I use: “j/ava_array = ruby_array.to_java :String/”, it works fine.

Can anyone help me of that problem?

I use: JRuby 1.1.4, Windows XP

Thanks a lot :slight_smile:

Marcin
Well, the problem here is that ruby_array is actually an array of
arrays. I think you would have to do something like this:

/ruby_array = [[1,2],[3,4]]/
//java_array = ruby_array.map{|arr| arr.to_java :double}.to_java
Java::double[][]

Not sure if this is exactly correct, actually…


Ola B. (http://olabini.com)
JRuby Core Developer
Developer, ThoughtWorks Studios (http://studios.thoughtworks.com)
Practical JRuby on Rails (http://apress.com/book/view/9781590598818)

“Yields falsehood when quined” yields falsehood when quined.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks Ola for quickly answer :slight_smile:

Hmm, when I use what you wrote I got an error: “'to_java’: wrong
argument
type #Class:01xc10de0 (expected Array)

Maybe is there any diffrent method to create a matrix in JRuby and after
that use it in JAMA (Java Matrix Package).

Thanks for help :slight_smile:

Marcin

On Fri, Oct 17, 2008 at 11:23 AM, Ola B. [email protected] wrote:

Can anyone help me of that problem?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Pozdrawiam
Marcin Piórkowski
Kom.: +48 506 606 648
http://www.piorkowski.in

“Jeżeli chcesz zmian, zacznij od siebie”