展平嵌套数组。 (JAVA) [英] Flatten nested arrays. (Java)

查看:141
本文介绍了展平嵌套数组。 (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创建合适的逻辑来压扁阵列。我基本上想要为嵌套数组中的每个子项复制父行。嵌套数组的数量可能会有所不同。我一直在创建Java列表bc我发现它们很容易使用,但对任何解决方案都是开放的。这个问题的本质是我从一些嵌套的JSON开始,我想将它转换成一个平面的csv加载到数据库表中。感谢您的帮助。

I'm struggling to create the right logic to flatten an array. I essentially want to duplicate parent rows for each child item in a nested array. The number of nested arrays could vary. I've been creating Java lists bc I find them easy to work with, but open to any solution. The nature of this problem is I'm starting with some nested JSON that I want to convert into a flat csv to load into a database table. Thanks for the help.

示例:

[1,2,[A,B,[Cat,Dog]],3]

我已经将上面创建为List。每个项目都是字符串或其他列表。

I've created the above as a List. Each item is either a string or another List.

结果:

[1,2,A,Cat,3],
[1,2,A,Dog,3],
[1,2,B,Cat,3],
[1,2,B,Dog,3]

这是我到目前为止所拥有的。显然不起作用。

Here's what I have so far. Obviously not working.

private static List<List<String>> processData(List<String> row, List<Object> data, List<List<String>> rowList) {
    List<List<String>> tempRowList = new ArrayList<List<String>>();
    for (Object i : data) {
        if (i instanceof List<?>) {
            flattenArray((List<Object>) i, row, rowList);
        } else {
            for (List<String> r : rowList) {
                r.add(i.toString()); //add item to all rows
            }
        }
    }
    return rowList;

private static void flattenArray(List<Object> arrayObject, List<String> rowToCopy, List<List<String>> rowList) {

    for (Object x : arrayObject) {

        if (x instanceof List<?>) {
            for (List<String> t : rowList) {
                flattenArray((List<Object>) x, t, rowList);
            }

        } else {
            List<String> newRow = new ArrayList<String>(rowToCopy); //copy row

            List<Object> t = new ArrayList<Object>();
            t.add(x);

            List<List<String>> innerRowList = new ArrayList<List<String>>();
            innerRowList.add(newRow);

            processData(newRow, t, innerRowList); //pass in new copied row. object to add, 
            rowList.add(newRow);

        }

    }
    rowList.remove(rowToCopy);
}

我设置了这样的一切。

And i set everything up like this.

public static void main(String[] args) {

    List<Object> data = new ArrayList<Object>();
    List<List<String>> rowList = new ArrayList<List<String>>();

    data.add("1");
    data.add("2");

    List<Object> l1 = new ArrayList<Object>();
    l1.add("A");
    l1.add("B");        
    List<Object> l2 = new ArrayList<Object>();
    l2.add("dog");
    l2.add("cat");
    l1.add(l2);
    data.add(l1);

    data.add("3");

    List<String> r0 = new ArrayList<String>();
    rowList.add(r0);
    System.out.println(data);
    rowList = processData(r0, data, rowList);
    System.out.println(rowList);
}


推荐答案

我认为这应该适用于如果您使用的是Java 8:

I think this should work for you if you're using Java 8:

    List<Object> a = new ArrayList<>();
    List<Object> a1 = new ArrayList<>();
    a1.add("v");
    a1.add("w");
    List<Object> a2 = new ArrayList<>();
    a2.add("ww");
    a.add("a");
    a.add("b");
    a.add("c");
    a.add("d");
    a.add(a1);
    a.add(a2);
    List<Object> b = new ArrayList<>();

    a.stream().flatMap(x -> x instanceof String ? Stream.of(x) : ((List) x).stream()).forEach(b::add);

    System.out.println(a);
    System.out.println(b);

b list将包含flattened a 列表。输出为:

b list will contain flattened a list. Output is:

[a, b, c, d, [v, w], [ww]]
[a, b, c, d, v, w, ww]

这篇关于展平嵌套数组。 (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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