List subList()的一个demo
只要保证
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的更多相关文章
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
- 初识nginx之第一个demo
商城项目做了一个多月了,想到必须用到负载均衡,简单了解了一下nginx,首先分享第一个demo,五月份上线后,会继续分享一系列相关知识. 在nginx根目录下,用了一个园友的批处理文件nginx.ba ...
- springMvc的第一个demo
1.下载jar包 http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.3.RELEASE/ 2.下载源码 j ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- 如何在WTL和MFC中使用duilib及如何静态使用duilib库!(初级讲解 附带一个Demo)
关于duilib的历史,我也就不多说了,能看到这篇文章的人都是有一定了解才能找到这个的. 我直接说下对这个库的基本使用吧. 我个人对一些好技术都是比较感兴趣的. 因为个人原因 喜欢接触一个好技术. 所 ...
- 白盒测试之gtest第一个demo
认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...
- 在VS中实现webService的一个demo(图解)
在VS中实现webService的一个demo(图解) 先创建一个web项目,创建好web项目后,添加新建项——web服务 在新建好的web服务文件中写如下代码: 生成当前解决方案. 新建一个winf ...
- Cocos2d-x 学习(1)—— 通过Cocos Studio创建第一个Demo
近期在工作上有了比較大的转变,自学情绪也慢慢高涨,本来一直在研究unity的技术.由于换了工作会開始接触cocos2d-x.但并不意味着停止研究unity,以后有时间还是会继续的. 公司的cocos2 ...
- 使用android的mediaplayer做成 一个demo,欢迎测试使用
附件是为一个定制视频产品而简单的写了一个demo,用来说明android的mediaplayer是如何使用的. http://files.cnblogs.com/guobaPlayer/palyerD ...
随机推荐
- 基于.net开发chrome核心浏览器【一】
原文:基于.net开发chrome核心浏览器[一] 说明: 这是本系列的第一篇文章,我会尽快发后续的文章. 源起 1.加快葬送IE6浏览器的进程 世界上使用IE6浏览器最多的地方在中国 中国使用IE6 ...
- ecshop加广告出现广告位的宽度值必须在1到1024之间的解决方法
ecshop加广告出现广告位的宽度值必须在1到1024之间,这点是非常郁闷的,因为现在宽屏网站逐渐成为主流,越来越多的宽屏网站进入了我们的视线,比如京东啊,一号店啊. 但是ecshop的广告位却不能超 ...
- 数据库的group by 分组
有一个表 查询结果为 用下面的代码写 select COUNT( case NumName when 'a' then NumName end ) as 'aaa', COUNT( case NumN ...
- PHP计算中文字符串长度 、截取相应中文字符串
PHP计算字符串长度 及其 截取相应中文字符串 计算字符长度: $gouWu = '美日汇http://www.hnzyxok.com/'; echo mb_strlen($gouWu,' ...
- 利用Sambaserver在Ubuntu系统和Win7系统间共享目录
1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...
- android升级软件版本号,您安装后的新版本号,成功安装画面没有出现,或直接回到桌面
Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //an ...
- MySQL 批量Dll操作(转)
概述 本章节介绍使用游标来批量进行表操作,包括批量添加索引.批量添加字段等.如果对存储过程.变量定义.预处理还不是很熟悉先阅读我前面写过的关于这三个概念的文章,只有先了解了这三个概念才能更好的理解这篇 ...
- C# 6.0 (C# vNext) 的新功能:Expression Bodied Functions and Properties
Expression Bodied Function 它可以用在: methods user-defined operators type conversions read-only properti ...
- 【HTML+CSS】(1)基本语法
HTML基金会 <em>他强调标签,<strong>加粗标签 <q>短文本引用.<blockquote>长文本引用,这两个标签会让文字带双引號. 空 ...
- ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤
务实直接上代码: 1. 重写FilterModule.cs using System; using System.Collections.Generic; using System.Linq; usi ...