0

I am looking for the rJava equivalent of:

String[] s;
s= new String[0];

I tried:

library(rJava)
.jinit()
s=.jarray(list(NULL), "[Ljava/lang/String;")

But when passing it to a method expecting a String[] with jcall(..., s), rJava raises an error.

Update

To make my question clearer.
I could, of course, easily make a new jar (or modifying the existing one for) hosting some zeroArray() method to be later called from R, but I am looking for a solution based on rJava, which means using standard Java objects or the classes in jar files shipped by rJava or internal rJava functions.

1 Answer 1

1

If I have something like this:

package utils;

public class RUsingStringArray {

        public void useArray(String [] array) {
          for(int i=0; i<array.length; i++ ) {
            String str = array[i];
          }
        }
        public int arrayLen(String [] array) {
                System.out.println("Class: " + array.getClass());
                return array.length;
        }
        public String [] createArray() {
                return new String[0];
        }
        public static void main(String [] arg) {

                RUsingStringArray obj = new RUsingStringArray();
                obj.useArray(obj.createArray());
                System.out.println("Len: " + obj.arrayLen(new String[0]) );
        }
}

method, createArray will return

> obj <- .jnew("utils.RUsingStringArray")
> s <- .jcall(obj, returnSig="[Ljava/lang/String;", method="createArray")
> s
character(0)

and you can use it as empty String [] later on

.jcall(obj, returnSig="V", method = "useArray", s)

Of course, this one will work as well

> b <- character(0)
> .jcall(obj, returnSig="V", method = "useArray", b)

Question is, whether this is something you are looking for.

Update:

In that case, maybe this is something better in your case?

> array <- .jarray(list(NULL), "java/lang/String")
> .jcall(obj, returnSig="V", method="useArray", array)

Update2:

How about this one ;)

> array2 <- .jarray(character(0), "java/lang/String")
> .jcall(obj, returnSig="I", method="arrayLen", array2)
Class: class [Ljava.lang.String;
[1] 0
>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I am actually looking for an rJava solution. I made this point clearer in the post update. Sorry if this was not clear in the beginning.
Testing with: public int useArray (String[] ar ) {return ar.length ;} and calling .jcall(obj, returnSig="I", method="useArray", array) returns 1, while it should be 0 if String[] array=new String[0];.
.oOo. yet another one, from my side ;) .oOo.
Great! and apparently simple after you know the sol)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.