Iterator和ListIterator的区别
来源:互联网 发布:freescale单片机 编辑:程序博客网 时间:2024/05/21 15:44
package com.yezi.learn_list;import java.util.ArrayList;import java.util.Iterator;import java.util.ListIterator;/*** * iterator对数据只能是单向的进行迭代模式 * ListIterator提供了双向的方式进行迭代模式 */public class TestArrayList { public static void main(String[] args) {// testCheckForComodification();// testListIterator(); testToArray(); } /*** * 使用Iterator和ListIterator都会报错 * 在进行迭代的时候不能,不能进行数据的修改 */ public static void testCheckForComodification(){ ArrayList<Integer> arrays = new ArrayList<Integer>(); arrays.add(1); arrays.add(2); Iterator<Integer> iterator = arrays.iterator(); while(iterator.hasNext()){ //arrays.add(1); //会修改ArrayList中的modCount的值,会抛出异常 Integer integer = iterator.next(); //使用自身的remove不会,查看源码,在remove前调用checkForComodification() //remove完后会将迭代器中的expectedModCount重新修改 iterator.remove(); } } /*** * 1、拥有Iterator的所有功能 * 2、提供反向迭代功能 * 3、本测试之测试反向迭代功能 */ public static void testListIterator(){ ArrayList<Integer> arrays = new ArrayList<Integer>(); arrays.add(1); arrays.add(2); ListIterator listIterator = arrays.listIterator(); //没有输出结果,是该迭代不是从最后一个开始迭代 while(listIterator.hasPrevious()){ System.out.println(listIterator.previous()); } //使用next将游标走到最后一条 while(listIterator.hasNext()){ //输出1,2 System.out.println(listIterator.next()); } //再次方向遍历 while(listIterator.hasPrevious()){ //输出2,1 System.out.println(listIterator.previous()); } } /*** * 测试T[] toArray(T[])方法 */ public static void testToArray(){ ArrayList<Integer> arrays = new ArrayList<Integer>(); arrays.add(1); arrays.add(2); Integer []array1 = new Integer[2]; Integer convers[] = arrays.toArray(array1); System.out.println(convers==array1); //输出true Integer []array2 = new Integer[1]; //当传递的length大小比list要小,就会使用Arrays.newInstance创建新的数组来 //包含装载数据 convers = arrays.toArray(array2); System.out.println(convers==array2); //输出false }}
0 0
- Iterator和listIterator的区别
- Iterator和listIterator的区别
- Iterator和listIterator的区别
- Iterator和ListIterator的区别
- Iterator和ListIterator的区别
- Iterator和ListIterator的区别
- Iterator和ListIterator的区别
- Iterator和ListIterator的区别
- Iterator和ListIterator的区别
- Iterator和ListIterator区别
- Iterator和ListIterator区别
- Iterator和ListIterator的作用和区别
- Iterator和ListIterator的作用和区别
- Iterator和listIterator的区别 迭代器
- Iterator和listIterator的区别 迭代器
- Iterator()和listIterator()的区别及应用
- java基础---Iterator和ListIterator的区别
- java中ListIterator和Iterator的区别
- opencv源码解析之(6):hog源码分析
- iOS基础学习路线:内容,资料,资源
- angular指令
- PowerDesigner设置为不自动根据name填充code值
- Windows 10 企业版(v9926)
- Iterator和ListIterator的区别
- OSG中的智能指针
- jquery 传参
- poj_3349 哈希
- Linux shell编程简介
- HDU-IMNU集训六:叠筐-纯模拟(注意技巧)
- 调用系统方法打电话
- ACM-HDU1789 Doing Homework again(又是贪心- -、)
- Codeforces Round #287 (Div. 2) D. The Maths Lecture (数位dp)