2

R call java interface issues.
I knew how to new a java user defined class object as well as call java function in R, some return values can be used directly in R, like integer, string, array, but I have no idea how to access the values of arraylist object.
For example:

public class Bond 
{
    public String compName;
    public long mfAmt;


    public Bond() {

    }
}

public class test_arr
{        
        public test_arr()
        { 
        }
        public ArrayList<Bond> getArrListDef()
    {
        ArrayList<Bond> arr = new ArrayList();
        Bond bond = new Bond();
        bond.compName = "app";
        bond.mfAmt = 12;
        arr.add(bond);
        return arr;
    }


        public ArrayList<Bond> getArrList(ArrayList<Bond> arr)
    {
        return arr;
    }
}

R call java part:

library(rJava)
test_arr = J('pkg.test_arr')
jarr = test_arr$getArrListDef()

now, the variable jarr is a Java-Object{}, so how can I print the value of jarr in R...also, how to passing a java arraylist object to another function "public ArrayList getArrList(ArrayList arr)".

1 Answer 1

2

You can always use the $ convenience operator. It provides an experimental, but simple way of writing code in Java style at the cost of speed. For example to print all elements mfAmt

for (index in seq(test_arr$size())-1)
  print(test_arr$get(as.integer(index))$mfAmt)
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much,I just did it,but something puzzled me also,becase the string type of return values is the Chinese characters encoding,thus,when I print this kind of variable,it is garbled,how can i do?
Could you please tell me why use operator $ could be very time consuming,but use .jcall(...) not?

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.