java - Merge arrylists and save as arraylist with in arraylist -
i have 3 array list , want merge array list 1 arraylist like, merge first object of arraylist 1 arraylist , put arraylist first object of new arraylist merge second object of arraylist 1 arraylist , put arraylist second object of new arraylist need output there possibility this
what asking transpose function.
here example using iterator
, can handle number of lists, , each list can different size.
list<list<integer>> input = arrays.aslist(arrays.aslist(1,2,3,4), arrays.aslist(5,6,7), arrays.aslist(8,9,10,11,12)); iterator<integer>[] iters = new iterator[input.size()]; int = 0; (list<integer> list : input) iters[i++] = list.iterator(); list<list<integer>> output = new arraylist<>(); while (true) { list<integer> sublist = new arraylist<>(iters.length); (iterator<integer> iter : iters) if (iter.hasnext()) sublist.add(iter.next()); if (sublist.isempty()) break; output.add(sublist); } system.out.println("input: " + input); system.out.println("output: " + output);
output
input: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]] output: [[1, 5, 8], [2, 6, 9], [3, 7, 10], [4, 11], [12]]
Comments
Post a Comment