只要保证
toIndex不大于size
并且
fromIndex不大于toIndex即可
(谁会传一个负数的下标呢?)

    public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
} static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
    public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("1");
int size = list.size();
System.out.println(size);
System.out.println(list.subList(0, size));
System.out.println(list.subList(size, size));
}
1
[1]
[]
import org.junit.Test;

import java.util.ArrayList;
import java.util.List; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is; /**
* Created by MyWorld on 2016/1/21.
*/
public class SubListDemoTest { @Test
public void ListSizeShouldEquals1_WhenSourceList9Step5() {
List<Integer> sourceList = new ArrayList<Integer>();
for (int i = 0; i < 9; i++) {
sourceList.add(i);
}
int step = 5;
List<String> targetList = SubListDemo.split(sourceList, step);
assertThat(targetList.size(), is(2));
} @Test
public void ListSizeShouldEquals2_WhenSourceList10Step5() {
List<Integer> sourceList = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
sourceList.add(i);
}
int step = 5;
List<String> targetList = SubListDemo.split(sourceList, step);
assertThat(targetList.size(), is(2));
} @Test
public void ListSizeShouldEquals3_WhenSourceList11Step5() {
List<Integer> sourceList = new ArrayList<Integer>();
for (int i = 0; i < 11; i++) {
sourceList.add(i);
}
int step = 5;
List<String> targetList = SubListDemo.split(sourceList, step);
assertThat(targetList.size(), is(3));
} }
import java.util.ArrayList;
import java.util.List; /**
* Created by MyWorld on 2016/1/21.
*/
public class SubListDemo {
public static List<String> split(List<Integer> list, int step) {
List<String> listStr = new ArrayList<String>();
int size = list.size();
int times = size / step + (size % step == 0 ? 0 : 1);
for (int i = 0; i < times; i++) {
int fromIndex = i * step;
int toIndex = (i + 1) * step;
listStr.add(list.subList(fromIndex, (toIndex < size ? toIndex : size)).toString());
}
return listStr;
}
}

tips:

java.util.List中有一个subList方法,用来返回一个list的一部分的视图。

List<E> subList(int fromIndex, int toIndex);

它返回原来list的从[fromIndex, toIndex)之间这一部分的视图,之所以说是视图,是因为实际上,返回的list是靠原来的list支持的。

所以,你对原来的list和返回的list做的“非结构性修改”(non-structural changes),都会影响到彼此对方。

所谓的“非结构性修改”,是指不涉及到list的大小改变的修改。相反,结构性修改,指改变了list大小的修改。

那么,如果涉及到结构性修改会怎么样呢?

如果发生结构性修改的是返回的子list,那么原来的list的大小也会发生变化;

而如果发生结构性修改的是原来的list(不包括由于返回的子list导致的改变),那么返回的子list语义上将会是undefined。在AbstractList(ArrayList的父类)中,undefined的具体表现形式是抛出一个ConcurrentModificationException。

因此,如果你在调用了sublist返回了子list之后,如果修改了原list的大小,那么之前产生的子list将会失效,变得不可使用。

tips: 如何删除一个list的某个区段,比如删除list的第2-5个元素?

方法是: 可以利用sublist的幕后还是原来的list的这个特性,比如

list.subList(from, to).clear();

这样就可以了。

示例代码:

public static void main(String[] args) {
List<String> parentList = new ArrayList<String>(); for(int i = 0; i < 5; i++){
parentList.add(String.valueOf(i));
} List<String> subList = parentList.subList(1, 3);
for(String s : subList){
System.out.println(s);//output: 1, 2
} //non-structural modification by sublist, reflect parentList
subList.set(0, "new 1");
for(String s : parentList){
System.out.println(s);//output: 0, new 1, 2, 3, 4
} //structural modification by sublist, reflect parentList
subList.add(String.valueOf(2.5));
for(String s : parentList){
System.out.println(s);//output:0, new 1, 2, 2.5, 3, 4
} //non-structural modification by parentList, reflect sublist
parentList.set(2, "new 2");
for(String s : subList){
System.out.println(s);//output: new 1, new 2
} //structural modification by parentList, sublist becomes undefined(throw exception)
parentList.add("undefine");
// for(String s : subList){
// System.out.println(s);
// }
// subList.get(0);
}

一个很有趣的思考:如何最高效的实现一个list的split方法?

参见:http://stackoverflow.com/questions/379551/java-split-a-list-into-two-sub-lists

http://www.cnblogs.com/gaojing/archive/2012/06/17/java-list-sublist-caution.html

List subList()的一个demo的更多相关文章

  1. angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”

    曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...

  2. 初识nginx之第一个demo

    商城项目做了一个多月了,想到必须用到负载均衡,简单了解了一下nginx,首先分享第一个demo,五月份上线后,会继续分享一系列相关知识. 在nginx根目录下,用了一个园友的批处理文件nginx.ba ...

  3. springMvc的第一个demo

    1.下载jar包 http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.3.RELEASE/ 2.下载源码 j ...

  4. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  5. 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)

    关于duilib的历史,我也就不多说了,能看到这篇文章的人都是有一定了解才能找到这个的. 我直接说下对这个库的基本使用吧. 我个人对一些好技术都是比较感兴趣的. 因为个人原因 喜欢接触一个好技术. 所 ...

  6. 白盒测试之gtest第一个demo

    认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...

  7. 在VS中实现webService的一个demo(图解)

    在VS中实现webService的一个demo(图解) 先创建一个web项目,创建好web项目后,添加新建项——web服务 在新建好的web服务文件中写如下代码: 生成当前解决方案. 新建一个winf ...

  8. Cocos2d-x 学习(1)—— 通过Cocos Studio创建第一个Demo

    近期在工作上有了比較大的转变,自学情绪也慢慢高涨,本来一直在研究unity的技术.由于换了工作会開始接触cocos2d-x.但并不意味着停止研究unity,以后有时间还是会继续的. 公司的cocos2 ...

  9. 使用android的mediaplayer做成 一个demo,欢迎测试使用

    附件是为一个定制视频产品而简单的写了一个demo,用来说明android的mediaplayer是如何使用的. http://files.cnblogs.com/guobaPlayer/palyerD ...

随机推荐

  1. 算法学习 - 图的广度优先遍历(BFS) (C++)

    广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不 ...

  2. Cocos2d-x3.0下一个 Lua与C++打电话给对方

    这里谈下Lua与C++如何实现相互通话 原来的连接:http://blog.csdn.net/qqmcy/article/details/26052771 DJLCData.h 实现类 // // D ...

  3. 21天教你学会C++

  4. TNS-12541: TNS:no listener TNS-12560: TNS:protocol adapter error TNS-00511: No listener Linux Error:

    今天是2014-06-17.遇到一个很奇怪的问题,可能之前測试改动监听的原因,导致监听启动后自己主动关闭,特此记录一下整个处理过程, 监听配置文件信息例如以下: [oracle@dg1 admin]$ ...

  5. 怎么样MyEclipse配置Tomcat?

    1.下载tomcat免安装版.tomcat路径不包含空格 http://download.csdn.net/detail/u014112584/7549191 2.windows -preferenc ...

  6. POJ 1475 Pushing Boxes 搜索- 两重BFS

    题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...

  7. ovs处理openflow消息的流程

    OVS处理各个openflow消息的详细代码在 ofproto/ofproto.c 中: static enum ofperr handle_openflow__(struct ofconn *ofc ...

  8. ios-html-get/post差额,简而言之(MS)CheckST

    get直接采取拉数据,post注射剂server.至server安全或使用get 而且由于get明确传递,password帐户A眼可以看得出来,甚至加密也可以很easy解,所以传password用po ...

  9. Java Tread多线程(0)一个简单的多线程实例

    作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39341887 本文演示,一个简单的多线程实例,并简单分析一下线程. 编程多 ...

  10. POJ 1905-Expanding Rods(二分法+计算几何)

    题目地址:POJ 1905 题意:一根某种材料做的直杆被夹在两面墙之间,当他受热时长度变长,就会因两面墙的挤压而向上隆起.长度变化函数为 L'=(1+n*C)*L,给定L,C,n,求向上拱起的高度H. ...