#2849 Instantiating [java]fanx.interop.:CharArray

Carlos Fri 16 Jul 2021

How should I create an instance of type [java]fanx.interop::CharArray?

brian Fri 16 Jul 2021

You just treat it like a class:

CharArray(10)

Will get compiled into

new char[10]

Carlos Fri 16 Jul 2021

I see. But how do I get it initialized?

I'm trying to instantiate a Java class that has a couple of constructors with the folowing signatures:

ZipFile(String)

ZipFile(String, char[])

I have no problem using the first constructor, but I need to use the second one and I can't make it to work. I have the value for the 2nd argument (the char[] one) stored in a Fantom's Str var and I need to convert it to a CharArray.

How should I do it?

brian Fri 16 Jul 2021

I would just use an explicit loop to convert a Fantom Str to a Java char[] as follows:

str := "hello world" 
array := CharArray.make(str.size)
str.each |char, i| { array[i] = char } 

There is a String.toCharArray however not accessible thru the Fantom Java FFI the way things work.

Carlos Fri 16 Jul 2021

I did it in that way. Thanks Brian!

Login or Signup to reply.