Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何初始化arraylist的arraylist_Java_Data Structures - Fatal编程技术网

Java 如何初始化arraylist的arraylist

Java 如何初始化arraylist的arraylist,java,data-structures,Java,Data Structures,下面的行将使用值为true的9个元素初始化arraylist public ArrayList<Boolean> timeTable = new ArrayList<Boolean>(Collections.nCopies(9, true)); publicArrayList时间表=新的ArrayList(Collections.nCopies(9,true)); 但如何初始化arraylist的arraylist public ArrayList<ArrayL

下面的行将使用值为true的9个元素初始化arraylist

public ArrayList<Boolean> timeTable = new ArrayList<Boolean>(Collections.nCopies(9, true));
publicArrayList时间表=新的ArrayList(Collections.nCopies(9,true));
但如何初始化arraylist的arraylist

public ArrayList<ArrayList<Boolean>> timeTable = new ArrayList<ArrayList<Boolean>>(Collections.nCopies(9, true));
公共数组列表
但不完全一样


场景是,我需要维护每日时间表的每月列表。现在每天的时间表只有9个条目,所以不可变就可以了。但每月的清单需要每个月追加。因此它不能是arraylist。

首先,建议尽可能使用接口类型。那会让你

ArrayList<ArrayList<Boolean>> -> List<List<Boolean>>. 
ArrayList->List。
然后,初始化语句将变为

public List<List<Boolean>> timeTable = Collections.nCopies(9, (Collections.nCopies(9, true)));
public List timeline=Collections.nCopies(9,(Collections.nCopies(9,true));

给定java文档中的这一行:“返回由指定对象的n个副本组成的不可变列表”

publicArrayList时间表=新的ArrayList(Collections.nCopies(9,true));
publicArrayList timeTableList=新的ArrayList(Collections.nCopies(9,timeline));

如果需要,不保证nCopies会返回一个ArrayList。我完全同意,但我怀疑ArrayList是否真的是一个要求,列表在这里是不够的。情况是,我需要维护一个每日时间表的月度列表。现在每天的时间表只有9个条目,所以不可变就可以了。但每月的清单需要每个月追加。所以它不可能是arraylist。场景是我需要维护一个每日时间表的月度列表。现在每天的时间表只有9个条目,所以不可变就可以了。但每月的清单需要每个月追加。所以它不可能是arraylist。所以你是说timeTableList应该是可变的。而不需要使用for循环
public ArrayList<Boolean> timeTable = new ArrayList<Boolean>(Collections.nCopies(9, true));

public ArrayList<ArrayList<Boolean>> timeTableLists = new ArrayList<ArrayList<Boolean>>(Collections.nCopies(9, timeTable));