您的位置:首页 > 编程语言

数组分组代码模拟

2016-08-17 20:09 375 查看
<span style="font-size:14px;">/**
* 数据分组 作用项目: 表情分组时数据拆分
*
* @author syusikoku
* @date 2016-8-17
*/
public class DataSplitGroup2 {

private static ArrayList<String> sourceList;
/**
* 1 -- 60
*/
private static int allMaxDatas = 37;
// 每组20条
private static int maxCount = 20;
private static Map<Integer, List<String>> cacheGroupList;
private static int startIndex;
private static int endIndex;

public static void main(String[] args) {
sourceList = new ArrayList<String>();
initDatas();
splitDatas();
printMsg();
}

private static void printMsg() {
Set<Entry<Integer, List<String>>> entrySet =
cacheGroupList.entrySet();
for (Entry<Integer, List<String>> entry : entrySet) {
System.out.println(
"pos:" + entry.getKey() + ",list:" + entry.getValue());
}
}

private static void splitDatas() {
// TODO Auto-generated method stub
// 每组20条
int latMaxSize = sourceList.size() / maxCount;
cacheGroupList = new HashMap<Integer, List<String>>();
if (sourceList.size() % maxCount == 0) {
for (int i = 0; i < latMaxSize; i++) {
startIndex = i * maxCount;
endIndex = (i * maxCount) + maxCount;
cacheGroupList.put(i,
sourceList.subList(startIndex, endIndex));
}
} else {
// 能够整除的数据
for (int i = 0; i < latMaxSize; i++) {
startIndex = i * maxCount;
endIndex = (i * maxCount) + maxCount;
cacheGroupList.put(i,
sourceList.subList(startIndex, endIndex));
}
// 需要新增加一页
// 最后一页的数据
cacheGroupList.put(cacheGroupList.size(),
sourceList.subList(endIndex, sourceList.size()));

}
}

private static void initDatas() {
for (int i = 0; i < allMaxDatas; i++) {
sourceList.add("itcast :" + i);
}
}

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: