ArrayList 进阶方法之ListIterator
同样看的都是jdk1.8 中 ArrayList中的源码,整理测试一下而已
ListIterator(int index)方法,返回指定下标(包含该下标)后的值,此时index位置的元素就是新列表迭代器的第一个值。是不是感觉有点像substring(intindex)?
注:ArrayList类同时还提供了 listIterator() 方法,此方法与listIterator(int index)的差异是index=0,
此方法可以将ArrayList转换成ListIterator.
下面是源码及测试代码
源码:
/**
* Returns a list iterator over the elements in this list (in proper
* sequence), starting at the specified position in the list.
* The specified index indicates the first element that would be
* returned by an initial call to {@link ListIterator#next next}.
* An initial call to {@link ListIterator#previous previous} would
* return the element with the specified index minus one.
*
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
测试代码:
import java.util.ListIterator; public class listIteratorTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
ListIterator<String> a = list.listIterator(2);
while (a.hasNext()) {
System.out.println(a.next());
} }
} 运行结果:
c
d
e
ArrayList 进阶方法之ListIterator的更多相关文章
- NSIS脚本入门和进阶方法
NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序.它提供了安装.卸载.系统设置.文件解压缩等功能.对于新手来说,它有 ...
- 编写测试类,了解ArrayList的方法
这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...
- 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold&q ...
- ArrayList.subList方法使用总结
ArrayList.subList方法使用总结 示例 List<String> list=new ArrayList<>(); list.add("d"); ...
- LinkedList方法总结 ListIterator和Iterator的区别
LinkedList也像ArrayList一样实现了基本的接口,但是它执行某些从操作时比ArrayList更高效,但在随机访问方面要逊色一些.LinkedList中有一些方法虽然名字不同,但可以完成相 ...
- 遍历Arraylist的方法:
遍历Arraylist的几种方法: Iterator it1 = list.iterator(); while(it1.hasNext()){ System.out ...
- 遍历Arraylist的方法
package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; publ ...
- ArrayList 冷门方法
以下代码片都是 jdk1.8 ArrayList中的官方代码 /** * Constructs a list containing the elements of the specified * co ...
- 集合Arraylist的方法的使用和打印
package chapter090; import java.util.ArrayList;import java.util.List; public class TestList01 { publ ...
随机推荐
- 深入理解 JavaScript 异步系列(5)—— async await
第一部分,ES7 中引入 async-await 原文地址 http://www.cnblogs.com/wangfupeng1988/p/6532734.html 未经作者允许,不得转载~ 前面介绍 ...
- python list 切片实验
list[start:stop:step] >>> a_list=['hito','bb','cc','dd','ee','ff']>>> a_list[::-1] ...
- Java基础之路(四)--流程控制语句
本次我们来聊一聊Java当中的循环语句. 循环语句分三种:1.for2.while3.do--while. 三种循环语句的任务是不同的,方法也是不同的.当然他们各自的流程图也是不一样的. 3.1 wh ...
- 构建微服务(Building Microservices)-PDF 文档
闲时翻译了几篇基于Spring Cloud.Netflix OSS 构建微服务的英文文章,为方便分享交流,整理为PDF文档. PDF 文档目录: 目录 一.微服务操作模型... 3 1. 前提 ...
- Windows Phone下页面跳转动画的实现
写在前面的一些废话: 前段时间一直忙于其他的事情,好长时间没有更新博客,很多东西虽然看过.学过,但是没有仔细去思考,去总结,长时间不用或者用的少难免会遗忘.最近由于家里以及感情方面的事,人也变得有点怨 ...
- Cesium原理篇:3D Tiles(3)个人总结
个人结论:目前,在演示层面,3D Tiles问题不大,但项目应用上就不够成熟了,所以问问自己,你是想吃瓜呢还是想吃螃蟹? 好的方面 数据规范 我非常喜欢glTF的整体设计,概括有四点:第一,数据块(B ...
- ARC引用计数
NSlog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)self)); block保环流---> ...
- UICollectionView 适配 iPhone 7 Plus
UICollectionView 适配 iPhone 7 Plus 需求:在屏幕上水平放置 5 张正方形图片,每张图片的宽度相等,无缝隙排列铺满一个屏幕宽度. 看似很简单的需求.用 UICollect ...
- webstorm入手笔记
一.webstorm学习前小记 webstorm是一款现在前端用的比较多的IDE,其优势也比较多,这个大家在网上随便搜搜就可以找到了.但是本人大部分的时间都是使用sublime text工作,最近由于 ...
- git pull错误记录及解决
执行操作:$ git pull 返回错误: error: RPC failed; result=7, HTTP code = 0 fatal: The remote and hung up unexp ...