Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize arraylist of arraylist

Following line will initialize arraylist with 9 elements with value true.

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

But how can i initialize arraylist of arraylist?

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

It should mean that outer arraylist has 9 inner arraylist and each inner arraylist has 9 elements with true value.

Similar to How can I initialize an ArrayList with all zeroes in Java? But not exactly same...

Scenario is that i need to maintain a monthly list of daily timetables. Now daily timetable will have only 9 entries, so immutable is fine. But monthly list needs to be appended each month. So it can't be an arraylist.

like image 943
rajya vardhan Avatar asked Feb 17 '26 01:02

rajya vardhan


1 Answers

Given this line form java docs: "Returns an immutable list consisting of n copies of the specified object"

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

public ArrayList<ArrayList<Boolean>> timeTableLists = new ArrayList<ArrayList<Boolean>>(Collections.nCopies(9, timeTable));
like image 50
Razvan Avatar answered Feb 18 '26 14:02

Razvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!