如何获取List集合中指定下标之间的数据
本篇文章为大家展示了如何获取List集合中指定下标之间的数据,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

贵南ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
需求
获取获取List集合中,指定下标之间的数据。
代码实现
测试:在list中添加数据0-10;获取下标为2和8之间的数据
@Test
public void test() {
List list = new ArrayList<>();
for (int i = 0; i <= 10; i++) {
list.add(i);
}
List result = getDataBetweenIndex(list, 2, 8);
for (Integer res : result) {
System.out.println(res);
}
} 获取数据的方法:
public ListgetDataBetweenIndex(List list, int startIndex, int endIndex){ int size = startIndex < endIndex ? endIndex - startIndex + 1 : list.size() - (startIndex - endIndex) + 1; int index = startIndex; List result = new ArrayList (); for (int i = 0; i < size; i++) { if (index == list.size()) { index = 0; } result.add(list.get(index)); index++; } return result; }
测试结果: 2,3,4,5,6,7,8
startIndex和endIndex反过来
8,9,10,0,1,2
上述内容就是如何获取List集合中指定下标之间的数据,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
新闻名称:如何获取List集合中指定下标之间的数据
标题路径:http://www.jxjierui.cn/article/gjjodi.html


咨询
建站咨询
