java - How to split an ArrayList<E> into ArrayList<ArrayList<E>>? -
i'm pretty new java , trying create arraylist<arraylist<integer>>
arraylist<integer>
(splitting 28800 data points pieces of 300).
public static arraylist<arraylist<integer>> createlistofarrays(arraylist<integer> list) { arraylist<arraylist<integer>> listofarrays = new arraylist<arraylist<integer>>(); arraylist<integer> temp = new arraylist<integer>(); (int = 0; < list.size(); i++) { int arrayint = list.get(i); temp.add(arrayint); if (temp.size() % 300 == 0) { listofarrays.add(temp); temp.clear(); } } return listofarrays; system.out.println(listofarrays.size()); system.out.println(listofarrays.get(0).size()); }
my print statements give me right size of 96 arraylists
empty. if remove temp.clear()
consist of 28800 integers
.
i need 96 * 300
different arraylists
in arraylist
.
for (int = 0; < list.size(); i++) { integer arrayint = list.get(i); temp.add(arrayint); if (temp.size() % 300 == 0) { listofarrays.add(temp); temp = new arraylist<>(); } }
dont use primitive
int
while retrieving element list. because unnecessarily causeunboxing
,autoboxing
again.rather relying on
list.size() % 300 == 0
, should change logic also.
you can use arraylist
method sublist
.
Comments
Post a Comment