Java 将基元数组转换为字符串的ArrayLIst

Java 将基元数组转换为字符串的ArrayLIst,java,arrays,reflection,arraylist,Java,Arrays,Reflection,Arraylist,我有一个方法,它转换调用方法后得到的对象(原语或字符串的数组)(在java.lang.Object实例中也是如此)。我想从输入数组构建ArrayList。 我尝试使用新的ArrayList 我已经编写了这样的代码 private List<String> getListOfStringForPrimitives( Object inputObject ) { Class<?> inputClass = null; if (inputObject != n

我有一个方法,它转换调用方法后得到的对象(原语或字符串的数组)(在java.lang.Object实例中也是如此)。我想从输入数组构建ArrayList。 我尝试使用新的ArrayList 我已经编写了这样的代码

private List<String> getListOfStringForPrimitives( Object inputObject ) {
     Class<?> inputClass = null;
    if (inputObject != null) {
        inputClass = inputObject.getClass();
    // Returns true if the inputObject is an Array
        if (isTypeAnArray(inputClass.getName())) {
            Class<?> componentType = inputClass.getComponentType();
            // If the inputObject is array of primitives build the list of Strings from the inputObject
            if (isTypePrimitive(componentType.getName())) {
                //ArrayList <String> arryList;
               // build an array list of Strings. from the inputObject and return.
            }
        }
    }
    return arryList;
private List getListOfStringForPrimitives(对象inputObject){
类inputClass=null;
if(inputObject!=null){
inputClass=inputObject.getClass();
//如果inputObject是数组,则返回true
if(isTypeAnArray(inputClass.getName())){
Class componentType=inputClass.getComponentType();
//如果inputObject是基元数组,则从inputObject构建字符串列表
if(isTypePrimitive(componentType.getName())){
//arryList arryList;
//构建字符串数组列表。从inputObject返回。
}
}
}
返回arryList;
}

我被困在如何从输入数组构建ArrayList!
提前感谢。

使用
java.lang.reflect.Array
从基元数组中获取基元值,然后将装箱的等价物(例如
整数
)添加到ArrayList中


编写一组if-then-else子句,将componentType与
Integer.TYPE
Character.TYPE
等进行比较。在每个子句中,编写对数组类的适当调用以获取原语,然后将它们传递给which.valueOf,然后将结果粘贴到数组列表中。

只需使用isArray方法:

如果我们知道数组的类型是int,我们可以这样做。在我的例子中,它可以是int、float、double short或boolean、String(object)。我在前面的方法中动态地得到了一个类型,我们不知道数组的类型是什么,所以写一个大的if/then/else语句。只有一组固定的基本类型。你需要为每一行编写不同的代码。我不确定我们是否能做到这一点。你能显示代码吗?复杂性又是什么呢?放置大if/else?@bmarglies:你是说使用Array.getInt(inputObject,index)吗?如何获得阵列的长度?我无法将inputObject强制转换为Object[]并使用.length属性。@sma语言就是这样的。是的,看起来有点乱。不,我不知道有什么捷径。当你深入思考时,你会得到很多代码。从[1,2,3,4]这样的输入数组中构建字符串的arrayList会有什么帮助?