如何将通用对象转换为原始数组?

时间:2016-02-25 02:59:59

标签: java arrays generics

单维原始数组在通用方法上无法正常工作。 new int []应该作为T []而不是T传递。感谢isArray(),我能够区分数组,但在尝试执行Arrays.deepToString()时遇到问题。如何将通用对象转换为基本数组?

/* Output
println(T $obj)
1
println(T[] $obj)
[1]
println(T[] $obj)
[[1]]
println(T[] $obj)
[[[1]]]
println(T $obj)
1
println(T $obj)
Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
    at Test.doSomething(Test.java:21)
    at Test.main(Test.java:12)
Java Result: 1
*/

import java.util.Arrays;

public class Test {

    public static void main(String[] args) throws Exception {
        doSomething((Integer) 1);
        doSomething(new Integer[]{1});
        doSomething(new Integer[][]231  options msglevel=i;
232  data x;
233   merge D1 D2;
234   by ID;
235   put (ID Date Amount)(=);
236  run;

INFO: The variable Date on data set WORK.D1 will be overwritten by data set WORK.D2.
ID=x1 Date=10/12/2015 Amount=100
ID=x2 Date=10/12/2016 Amount=200
ID=x2 Date=. Amount=150
ID=x3 Date=10/10/2014 Amount=90
ID=x4 Date=01/01/2016 Amount=60
);
        doSomething(new Integer[][][]{237  data want;
238    merge d1 (keep=ID Date Amount rename=(Date=Date1))
239          d2 (keep=ID Date rename=(Date=Date2))
240    ;
241    by ID;
242    Date=coalesce(Date1,Date2);
243    put (ID Date1 Date2 Date Amount)(=);
244    format Date mmddyy10.;
245  run;

ID=x1 Date1=10/12/2015 Date2=. Date=10/12/2015 Amount=100
ID=x2 Date1=. Date2=10/12/2016 Date=10/12/2016 Amount=200
ID=x2 Date1=. Date2=10/12/2016 Date=10/12/2016 Amount=150
ID=x3 Date1=10/10/2014 Date2=. Date=10/10/2014 Amount=90
ID=x4 Date1=. Date2=01/01/2016 Date=01/01/2016 Amount=60
});

        doSomething((int) 1);
        doSomething(new int[]{1}); //java.lang.ClassCastException
        doSomething(new int[][]select substr(name, -1, 1) as character, count(*) as wordscount from names              
group by substr(name, -1, 1) having count(*)= (  
    select min(count(*)) from   names              
     group by substr(name, -1, 1)
 )
);
        doSomething(new int[][][]{select * from (
select substr(name, -1, 1) as character, count(*) as wordscount from names              
group by substr(name, -1, 1) ) A  where wordscount = (  
    select min(count(*)) from   names              
     group by substr(name, -1, 1)
 ) 
});
    }

    public static <T> void doSomething(T $obj) {
        System.out.println("println(T $obj)");

        if ($obj.getClass().isArray()) {
            System.out.println(Arrays.deepToString((T[]) $obj)); //java.lang.ClassCastException
        } else {
            System.out.println($obj);
        }
    }

    public static <T> void doSomething(T[] $obj) {
        System.out.println("println(T[] $obj)");

        if ($obj.getClass().isArray()) {
            System.out.println(Arrays.deepToString($obj));
        } else {
            System.out.println($obj);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你做不到。 Arrays.deepToString()接受参数Object[],其中基元数组类型不是子类型。如果你知道你有一个基元数组,你可以简单地在它上面调用Arrays.toString()(因为基元数组不能“深入”),这对于所有基本类型数组都是重载的。

唯一的另一种情况是你有一些可能是一个基元数组或一个引用数组,你不知道哪个,如果它是一个引用数组,你想要递归地打印它。您可以编写自己的方法,根据每个基元类型类型手动检查对象的类型,如果是,则将其转换为正确的类型并在其上调用Arrays.toString(),如果它是数组引用,将其转换为Object[]并在其上调用Arrays.deepToString()

或者,如果你是懒惰的,你可以在一个额外的数组层中包装任何内容,并对其进行Arrays.deepToString()

Arrays.deepToString(new Object[]{$obj})

然后再删除额外的括号集。