java - Efficient way to divide a list into lists of n size -


i have array, want divide smaller arrays of n size, , perform operation on each. current method of doing is

implemented arraylists in java (any pseudocode do)

    (int = 1; <= math.floor((a.size() / n)); i++) {             arraylist temp = subarray(a, ((i * n) - n),                     (i * n) - 1);             // stuff temp         }      private arraylist<comparable> subarray(arraylist a, int start,                 int end) {             arraylist toreturn = new arraylist();             (int = start; <= end; i++) {                 toreturn.add(a.get(i));             }             return toreturn;         } 

where list, n size of desired lists

i believe way taking time when working considerably large lists (of 1 million in size) i'm trying figure out more efficient.

you'll want makes use of list.sublist(int, int) views rather copying each sublist. easily, use guava's lists.partition(list, int) method:

list<foo> foos = ... (list<foo> partition : lists.partition(foos, n)) {   // partition } 

note this, many things, isn't efficient list isn't randomaccess (such linkedlist).


Comments

Post a Comment

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -