List拆分多个list

import java.util.ArrayList;
import java.util.List;

/**
* 描述:List拆分操作
*
* @auther husheng
* @date 2020/5/23 1:42
*/
public class ListOperation {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 11; i++) {
list.add(i);
}
System.out.println(list.toString());//[1, 2, 3, 4, 5, 6, 7, 8, 9, 11]

/**
* 对list进行拆分,比如要调用的接口的入参是list,但是限制了大小是2,
* 也就是说一次只能传2条数据
*/

int size = 2; //定义要拆分的单个list内的元素
/**
* times 表示将对list进行几次拆分
* list的大小%size 如果能除尽,times的循环次数为list.size()%size
* list的大小%size,如果除不尽,times的循环次数需要多一次, list.size() / size + 1
*/
int times = list.size() % size == 0 ? list.size() / size : list.size() / size + 1;

for (int i = 0; i < times; i++) {
//tempList存放每一次需要当做参数操作check方法的集合
List<Integer> tempList = new ArrayList<Integer>();

//将指定索引数据放入的tempList中
for (int j = i * size; j <= size * (i + 1) - 1; j++) {
if (j <= list.size() - 1) {
tempList.add(list.get(j));
}
System.out.println("tempList: " + tempList);

}
//调用方法
check(tempList);
}
}

/**
* 遍历list操作,如果list内的个数大于2,则无法进行遍历操作
*
* @param list
*/
static int i = 1;

public static void check(List<Integer> list) {

if (list.size() > 2) {
System.out.println("入参大于2,无法进行下一步操作");
return;
}
System.out.println("第" + (i++) + "次循环......");
System.out.println("遍历list开始......");
System.out.println("list-size----" + list.size());
System.out.println("list----" + list);
System.out.println("遍历结束");
System.out.println("-------------------");

}

}




猜你喜欢

转载自www.cnblogs.com/husheng1987hs/p/12940789.html
今日推荐