只要保证
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. java反射中Method类invoke方法的使用方法

    package com.zsw.test; import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetExcep ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

  3. Gulp实现服务器

    Gulp实现web服务器 Gulp实现web服务器 阅读目录 一:gulp实现web服务器配置: 二:添加实时刷新(livereload)支持 回到顶部 一:gulp实现web服务器配置: 对于前端开 ...

  4. 《HBase权威指南》读书笔记----简介

    工作中要使用HBase,刚刚开始接触HBase,理解不深,只是记录一下 . HBase基于google的bigtable论文实现,属于nosql. 几个概念: (1)列(column):最基本单位为列 ...

  5. 尝到awk

    我前几天写的sed,这个时候继续了解它的兄弟,awk,两者都使用,一种感觉.既可以用来处理场.假设你想要做文本处理.sed删除.匹配,一些频繁更换使用,假设每一行文本,你想深入,一些每行和列处理的,例 ...

  6. Android从无知到有知——NO.7

    的ip拨号器在监听外拨电话时用的是系统提供的广播事件.而有些时候我们须要自己设定广播事件来满足特定的须要. Ok,今天整一下自己定义广播事件,我们用一个状态监測模块向一个3G模块发送报警信息来实现这一 ...

  7. Javascript中的__proto__、prototype、constructor

    今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...

  8. centos7关闭防火墙(转)

    直接命令:service firewalld stop 1. Disable Firewalld Service. [root@rhel-centos7-tejas-barot-linux ~]# s ...

  9. 让你的字ScrollView、ListView充分伸展

    android默认ScrollView.ListView在最顶部的下拉上拉时或底部,未与反弹效应,很僵,让你无法继续拖累,不比iOS至于能否反弹.个人觉得,iOS互动还是略胜一筹.因此,我们已经走在A ...

  10. 【Jqurey EasyUI+Asp.net】---DataGrid增加、删、更改、搜

    在前面写了两,但不知道如何完成,对比刚刚开始学这个,他们摸着石头过河,一步步.在最后两天DataGridCRUD融合在一起.因此份额.我希望像我这样谁是刚刚开始学习一些帮助. 直接主题酒吧. 它是说数 ...