2 回答
TA贡献1845条经验 获得超8个赞
为我自己的帖子找到了解决方案。为了将来参考,原来有一个 Maven Jackson 依赖项可以做到这一点
@Autowired
com.fasterxml.jackson.databind.ObjectMapper objectMapper;
//...other methods and variables
private String ObjArrayDisplay(Object o, Class c){
String returnValue = null;
try {
returnValue = objectMapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return returnValue;
}
TA贡献1803条经验 获得超6个赞
如果我理解正确,您可以使用包装类并将其打印为 toString
class Scratch {
public static void main(String[] args) {
Double[] objects = {2.02,2.22,2.13};
System.out.print(new Foo(objects));
String[] strings = {"array","of", "strings"};
System.out.print(new Foo(strings));
}
public static class Foo{
private Object[] objects;
public Foo(Object[] objects){
this.objects=objects;
}
@Override
public String toString() {
return "Foo{" +
"objects=" + Arrays.toString(objects) +
'}';
}
}
}
您提到您无法控制它是否是基元,但它们不会被传递,private void othermethod(Class[] clazz, Object[] obj)因为它们不是对象数组。
添加回答
举报