Java从包含Intellij中包含双精度数组的对象的对象数组中访问数组元素

背景故事:我有一个 Spring Boot 应用程序,它可以获取使用自定义注释进行注释的任何方法的输入参数。它将这些参数的类型放在 Class Arrayclazz中,并将值放在 Object 数组obj中。


这是通用的,因此 obj 中的元素可以是数组,例如:


clazz = {int, java.lang.String, [D}

obj = {2, "Foobar", [3.1415, 2.718]}

我正在尝试使用以下代码打印出 obj 中的所有元素。请注意, clazz 和 obj 始终具有相同的长度,并且相同的索引彼此相关(参见上面的示例)。


代码检查对象是否是一个数组,而不是打印类似 [D@431621


private void othermethod(Class[] clazz, Object[] obj){


    for(int i =0 ; i<clazz.length;i++){

        if(clazz[i].isArray()){

            System.out.println(ObjArrayDisplay(obj[i], clazz[i]));

        }

        else{

            System.out.println(obj[i].toString());

        }

    }

}


private String ObjArrayDisplay(Object o, Class c){

    //Option 1. return Arrays.deepToString((Object[])o);java.lang.ClassCastException: [D cannot be cast to [Ljava.lang.Object;

    //Option 2. return Arrays.deepToString(c.cast(o)); deepToString (java.lang.Object[]) in Arrays cannot be applied to (java.lang.Object)

}

这就是问题所在。


让我们回到示例值:


clazz = {int, java.lang.String, [D}

obj = {2, "Foobar", [3.1415, 2.718]}

选项 1:对于这个 clazz 和 obj,代码可以编译,但是当代码到达 Double 数组时,它不能被强制转换回 Object 数组,因为 Double 扩展了 Object。


选项 2:由于语法错误,这次代码甚至无法编译:Arrays.deepToString 需要一个 Object 数组,但在运行之前,语法检查器只能看到 Object。直到运行时之后,包装在 Object 中的 Double[] 才被发送到堆栈中。


这对于 Hashmap、ArrayList 等其他数据结构也会有问题。


有没有办法得到Object o的元素?我无法控制哪些原始类型或对象是注释方法的输入,因此更改 clazz 和 obj 不是一种选择。


编辑:提供 clazz(即类)和 obj(即参数)的 Spring Boot 类


@Aspect

@Component

@Getter

public class Flow {

    @Autowired

    SubscriptionsIntegration SI;


    private Class[] classes;

    private Object[] params;

    private Object returnvalue = "Start";


    @Around("@annotation(TrackFlow)")

    public Object TrackFlow(ProceedingJoinPoint joinPoint) throws Throwable{


        CodeSignature cs = (CodeSignature) joinPoint.getSignature();


        classes = cs.getParameterTypes();

        params = joinPoint.getArgs();


        Object proceed = joinPoint.proceed();


        returnvalue = proceed;


        return proceed;

    }

}

编辑 2:在一个不起眼的 maven 依赖项中找到了解决方案,并将其发布在这里。


30秒到达战场
浏览 99回答 2
2回答

精慕HU

为我自己的帖子找到了解决方案。为了将来参考,原来有一个 Maven Jackson 依赖项可以做到这一点@Autowiredcom.fasterxml.jackson.databind.ObjectMapper objectMapper;//...other methods and variablesprivate String ObjArrayDisplay(Object o, Class c){&nbsp; &nbsp; String returnValue = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; returnValue = objectMapper.writeValueAsString(o);&nbsp; &nbsp; } catch (JsonProcessingException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return returnValue;}

慕码人8056858

如果我理解正确,您可以使用包装类并将其打印为 toStringclass Scratch {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Double[] objects = {2.02,2.22,2.13};&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(new Foo(objects));&nbsp; &nbsp; &nbsp; &nbsp; String[] strings = {"array","of", "strings"};&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(new Foo(strings));&nbsp; &nbsp; }&nbsp; &nbsp; public static class Foo{&nbsp; &nbsp; &nbsp; &nbsp; private Object[] objects;&nbsp; &nbsp; &nbsp; &nbsp; public Foo(Object[] objects){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.objects=objects;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Foo{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "objects=" + Arrays.toString(objects) +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您提到您无法控制它是否是基元,但它们不会被传递,private void othermethod(Class[] clazz, Object[] obj)因为它们不是对象数组。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java