java 使用subList List分割
list中有1000条数据,把1000分为20集合,每个分别打印出来,请教简单例子实现功能?
[解决办法]
public static void main(String[] args) { int count = 1000; int index = 0; List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < count; i++) { list.add(i+1); } for (int i = 0; i < 20; i++) { List<Integer> temp = list.subList(index, index+count/20); index += count/20; System.out.println("the data of list " + (i + 1) + "===========>"); for (int j = 0; j < temp.size(); j++) { System.out.println(temp.get(j)); } } }
[解决办法]
List<Integer> list=new ArrayList<Integer>(); for(int i=1;i<1001;i++) list.add(i); int count=list.size()/20; for(int i=0;i<20;i++){ List<Integer> subList= list.subList(i*count,count*(i+1)); System.out.println(subList); }