ruby - Convert instance of class org.jruby.RubyArray to class java.util.ArrayList -
i'm new ruby & jruby . want test stuffs of jruby in java code
here code :
import java.util.arraylist; import java.util.hashmap; import org.jruby.embed.localvariablebehavior; import org.jruby.embed.scriptingcontainer; public class test { public static void main(string[] args){ scriptingcontainer container = new scriptingcontainer(localvariablebehavior.persistent); test t = new test(); logstatbean bean = t.new logstatbean(); container.sethomedirectory("classpath:/meta-inf/jruby.home"); container.put("bean", bean); container.runscriptlet("arr = [1, 2, 3, 4, 5, 6]"); container.runscriptlet("puts arr"); container.runscriptlet("bean.setoutput(arr) "); system.out.println(bean.getoutput()); } public class logstatbean { public arraylist<hashmap<string, object>> getoutput() { return output; } public void setoutput(arraylist<hashmap<string, object>> output) { this.output = output; } public arraylist<hashmap<string, object>> output; } }
i cannot set java local variable type arraylist in jruby ,it raise error
typeerror: cannot convert instance of class org.jruby.rubyarray class java.util.arraylist (root) @ <script>:1
what have ?
array in ruby (usually) converts java array - either stop expecting arraylist or conversion in ruby ... piece of ruby should helpful :
>> [1, 2, 3].class => array >> [1, 2, 3].to_java.java_class => class [ljava.lang.object; >> [1, 2, 3].to_java('java.lang.integer').java_class => class [ljava.lang.integer; >> java.util.arraylist.new [1, 2, 3] => #<java::javautil::arraylist:0x1b802d73> >> java.util.arrays.aslist([1, 2, 3].to_java) => #<java::javautil::arrays::arraylist:0x10478ebc>
Comments
Post a Comment