如何将嵌套列表转换为多维数组? [英] How to convert nested List into multidimensional array?

查看:42
本文介绍了如何将嵌套列表转换为多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,我想将嵌套的 List 转换为该类型的多维数组,其中包含最深层的统一类型.例如,将 ArrayList>>> 转换为 String[][][][].我尝试了几件事,但我只能获得像 Object[][][][] 这样的对象数组.对于简单列表",似乎 Apache Commons Lang 可以完成这项工作,但我无法弄清楚嵌套的情况.

In Java I want to convert a nested List which contains at the deepest level a uniform type into an multidimensional array of that type. For example, ArrayList<ArrayList<ArrayList<ArrayList<String>>>> into String[][][][]. I've tried several things and I only can obtain an array of objects like Object[][][][]. For 'simple lists' it seems that Apache Commons Lang does the work but I cannot figure out for nested cases.

更新:

为了获得对象类型的多维数组,我使用了递归函数,因此无法使用 toArray() 设置键类型,请参阅摘录:

In order to obtain a multidimensional array of Object type I'm using a recursive function so I cannot set the key type using toArray() see excerpt:

// the argument of this function is a (nested) list
public static Object convert(Object object) {

    Object[] result = null;
    List list = (List) object;
    if (list != null) {

        Object type = getElementType(list);
        if (type instanceof List) {

            int size = list.size();
            result = new Object[size];
            for (int counter = 0; counter < size; counter++) {

                Object element = list.get(counter);
                result[counter] = (element != null) ? convert(element) : null;
            }
        } else {
            result = list.toArray();
        }
    }

    return result;
}

private static Object getElementType(List list) {

    Object result = null;
    for (Object element : list) {
        if (element != null) {

            result = element;
            break;
        }
    }

    return result;
}

推荐答案

这是有人建议解决String类型的方法.Cast2(List) 返回多维数组.可以推广使用类类型作为参数.感谢您的评论.

This is the way that someone suggested to solved for String type. Cast2(List<?>) returns the multidimensional array. It may be generalized to use the class type as parameter. Thank you for your comments.

static int dimension2(Object object) {

    int result = 0;
    if (object instanceof List<?>) {

        result++;
        List<?> list = (List<?>) object;
        for (Object element : list) {
            if (element != null) {
                result += dimension2(element);
                break;
            }
        }
    }

    return result;
}


static Object cast2(List<?> l) {

    int dim = dimension2(l);
    if (dim == 1) {
        return l.toArray(new String[0]);
    }

    int[] dims = new int[dimension2(l)];
    dims[0] = l.size();
    Object a = Array.newInstance(String.class, dims);
    for (int i = 0; i < l.size(); i++) {

        List<?> e = (List<?>) l.get(i);
        if (e == null) {
            Array.set(a, i, null);
        } else if (dimension2(e) > 1) {
            Array.set(a, i, cast2(e));
        } else {
            Array.set(a, i, e.toArray(new String[0]));
        }
    }
    return a;
}

这篇关于如何将嵌套列表转换为多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆