如何从Java对象(REXP对象)获取值?

时间:2017-02-05 11:03:46

标签: java r rserve

我试图解析REXP输出。我已将结果转换为java对象。但是我无法获得对象内的值。 对象m包含:

[1, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
[9.12324245]
[1212,1234,4343]
[3.456]

我需要将这些值放入数组中进行进一步处理

这里的任何帮助真的很棒吗?代码段如下:

REXP kmv = connection.eval(kmeans);
HashMap<String, Object> j = (HashMap<String, Object>) kmv.asNativeJavaObject();
Set<Entry<String, Object>> set = j.entrySet();
Iterator<Entry<String, Object>> i = set.iterator();
while(i.hasNext()) {
    Map.Entry<String, Object> me = (Map.Entry<String, Object>)i.next();
    String key = (String) me.getValue();
    Object m = (Object)me.getKey();              
}

1 个答案:

答案 0 :(得分:0)

答案很简单

             // Back to the basics of decoding an object :)
             if (m.getClass().isArray()) {
                 if (m instanceof double[]) {
                     value = Arrays.toString((double[])m);
                 } else if (m instanceof int[]) {
                     value = Arrays.toString((int[])m);
                 }
             }