可以使用 Collections 类的 nCopies 方法来创建指定个数的空集合。

import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int num = 5;
        List<List<Object>> emptyLists = Collections.nCopies(num, Collections.emptyList());
        System.out.println(emptyLists);
    }
}

输出结果为:

[[], [], [], [], []]

这样就创建了 5 个空集合。

Java 不使用for循环使用工具类根据一个数字创建 几个空集合

原文地址: https://www.cveoy.top/t/topic/i5Oy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录