lundi 23 février 2015

Splitting a list into groups



I have a list of strings and I was wondering if there was an easy way to join the elements in groups of size n, where the last elements would be automatically handled if less than n. For example,



String[] strings = {"A", "man", "a", "plan", "a", "canal", "Panama"};


The expected output for n = 3 would be the following list of strings. Remember, I can use some exhausting iteration to do this myself, but I'm looking for something simple. Perhaps something built-in to the standard library.



String[] output = {"A man a", "plan a canal", "Panama"};



Aucun commentaire:

Enregistrer un commentaire