Repository navigation
Expand file tree
/
Copy pathSplitListTest.java
More file actions
75 lines (57 loc) · 1.62 KB
/
Copy pathSplitListTest.java
File metadata and controls
75 lines (57 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.chen.test;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* @author : chen weijie
* @Date: 2019-08-29 14:57
*/
public class SplitListTest {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(12);
list.add(22);
list.add(42);
list.add(52);
list.add(62);
list.add(72);
list.add(82);
list.add(92);
list.add(102);
list.add(112);
list.add(122);
List<List<Integer>> resultList = splitList(list, 5);
for (List<Integer> integers : resultList) {
System.out.println(JSONObject.toJSONString(integers));
}
}
/**
* 将待邀请的成员列表分成n个组
*
* @param source
* @param n
* @return
*/
private static List<List<Integer>> splitList(List<Integer> source, int n) {
List<List<Integer>> result = new ArrayList<>();
//(先计算出余数)
int remaider = source.size() % n;
//然后是商
int number = source.size() / n;
//偏移量
int offset = 0;
for (int i = 0; i < n; i++) {
List<Integer> value;
if (remaider > 0) {
value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
remaider--;
offset++;
} else {
value = source.subList(i * number + offset, (i + 1) * number + offset);
}
result.add(value);
}
return result;
}
}