Java使用递归创建动态多维数组

时间:2016-09-27 23:00:30

标签: java arrays recursion multidimensional-array

谁可以帮助我使用递归创建动态多维数组。 我需要创建一个具有数组深度和子数

的函数

阵列的深度可能有不同的大小

class Item {
    public int id;
    public Map<Integer, Item>[] children;
}

Map<Integer, Item> items = new HashMap<>();

function void build(int depth, int countChildren) {
    ...
}

function Item createItem() {
    Item item = new Item();
    item.id = Math.Random(0,10000);

    return item;
}

Array看起来像这样

Image link

[
    [
        'id' => 1,
        'children' => [
            [
                'id' => 10,
                'children' => [
                    [
                        'id' => 100,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                    [
                        'id' => 200,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                ]
            ],
            [
                'id' => 20,
                'children' => [
                    [
                        'id' => 100,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                    [
                        'id' => 200,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                ]
            ],
        ]
    ],
    [
        'id' => 1,
        'children' => [
            [
                'id' => 10,
                'children' => [
                    [
                        'id' => 100,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                    [
                        'id' => 200,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                ]
            ],
            [
                'id' => 20,
                'children' => [
                    [
                        'id' => 100,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                    [
                        'id' => 200,
                        'children' => [
                            [
                                'id' => 1000,
                            ],
                            [
                                'id' => 2000,
                            ],
                        ]
                    ],
                ]
            ],
        ]
    ],
];

0 个答案:

没有答案