arraylist - Fixed SubList Count but Dynamic Members in Java -
i have following integer list
list<integer> arraylist = new arraylist<integer>(); (int = 0; < 7; i++) { arraylist.add(i); } so list [0,1,2,3,4,5,6]. scenario
if give value = 5 parameter split 5 sub list this
[0,5], [1,6] , [2], [3], [4] if give value = 4 parameter split 4 sub list this
[0,4], [1,5], [2,6] , [3] if give value = 3 parameter split 3 sub list this
[0,3,6], [1,4], [2,5] i tested below function not need.
public list<list<integer>> chopped(list<integer> list, final int splitcount) { list<list<integer>> parts = new arraylist<list<integer>>(); final int n = list.size(); (int = 0; < n; += splitcount) { parts.add(new arraylist<notification>(list.sublist(i, math.min(n, + splitcount)))); } return parts; } at above function, give splitcount 5 function returns
[0,1,2,3,4], [5,6] the result expect [0,5], [1,6] , [2], [3], [4]
how about:
public list<list<integer>> chopped(list<integer> list, final int splitcount) { list<list<integer>> parts = new arraylist<>(splitcount); (int = 0; < splitcount; ++i) { parts.add(new arraylist<>()); } final int n = list.size(); (int = 0; < n; ++i) { parts.get(i % splitcount).add(list.get(i)); } return parts; }
Comments
Post a Comment