Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { /** * Split a list into numFolds (roughly) equally sized folds. The earlier folds * may have one more item in them than later folds. */ public static <T> List<Collection<T>> partitionIntoFolds(List<T> values, int numFolds) { List<Collection<T>> folds = new ArrayList<Collection<T>>(); int numValues = values.size(); int foldSize = numValues / numFolds; int remainder = numValues % numFolds; int start = 0; int end = foldSize; for (int foldNum = 0; foldNum < numFolds; foldNum++) { // if we're in the first 'remainder' folds, we get an extra item if (foldNum < remainder) { end++; } folds.add(values.subList(start, end)); start = end; end += foldSize; } return folds; } }