listIterator(int index)方法

在List接口中,还有一个方法listIterator(int index)可以得到List的迭代器(ListIterator接口的对象),其中的参数index为迭代的起始位置,其它的同listIterator()方法得到的迭代器一样。


一、List接口

在Java中,List接口的定义如下:

[java]  view plain copy
  1. <span style="font-size:18px;">public interface List<E> extends Collection<E> {  
  2.     int size();  
  3.     boolean isEmpty();  
  4.     boolean contains(Object o);  
  5.     Iterator<E> iterator();  
  6.     Object[] toArray();  
  7.     <T> T[] toArray(T[] a);  
  8.     boolean add(E e);  
  9.     boolean remove(Object o);  
  10.     boolean containsAll(Collection<?> c);  
  11.     boolean addAll(Collection<? extends E> c);  
  12.     boolean addAll(int index, Collection<? extends E> c);  
  13.     boolean removeAll(Collection<?> c);  
  14.     boolean retainAll(Collection<?> c);  
  15.     void clear();  
  16.     boolean equals(Object o);  
  17.     int hashCode();  
  18.     E remove(int index);  
  19. //上面的方法继承自Collection接口,下面的方法是List接口本身的扩展  
  20.      E get(int index);  
  21.     E set(int index, E element);  
  22.     int indexOf(Object o);  
  23.     int lastIndexOf(Object o);  
  24.     ListIterator<E> listIterator();  
  25.     ListIterator<E> listIterator(int index);  
  26.     List<E> subList(int fromIndex, int toIndex);  
  27. }</span>  

从定义来看,List接口除了定义了一些对List的增加,删除,修改等操作外,还有就是里面有一个ListIterator接口,当对象调用listIterator()方法时,就会得到一个ListIterator接口的对象,该接口是干什么呢?

二、ListIterator接口

在Java中,ListIterator接口的定义如下:

[java]  view plain copy
  1. <span style="font-size:18px;">public interface ListIterator<E> extends Iterator<E> {  
  2.     boolean hasNext();  
  3.     E next();  
  4.     void remove();  
  5.     boolean hasPrevious();  
  6.     E previous();  
  7.     int nextIndex();  
  8.     int previousIndex();  
  9.     void set(E e);  
  10.     void add(E e);  
  11. }</span>  

从定义可以看出,ListIterator接口是Iterator的扩展。也就是说,实现ListIterator接口的类的对象都可以使用增强for循环进行遍历。我们都知道,hashNext()和next()方法是查找后继元素的时候使用,由此可想而知,ListIterator接口中的hasPrevious()和privious()方法是用来查找前驱元素的。同时,还扩展了一个set()方法。set() 方法传递的参数E的对象,该方法是干什么用的呢?

[java]  view plain copy
  1. <span style="font-size:18px;">package ljp.lists;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Collection;  
  6. import java.util.ListIterator;  
  7.   
  8. public class ListDemo01 {  
  9.     public static void main(String[] args) {  
  10.         List<Integer> arraylist = new ArrayList<Integer>();  
  11.         arraylist.add(1);  
  12.         arraylist.add(2);  
  13.         arraylist.add(3);  
  14.         arraylist.add(4);  
  15.         //查看arrayList中的内容  
  16.         for(int i : arraylist){  
  17.             System.out.println(i);  
  18.         }  
  19.         ListIterator<Integer> listit = arraylist.listIterator();  
  20.         listit.set(10);  
  21.         //使用过set()方法之后,查看有什么变化  
  22.         for(int i : arraylist){  
  23.             System.out.println(i);  
  24.         }  
  25.     }  
  26. }</span>  

运行上面的程序,在调用set()方法的时候,抛出IllegalStateException异常。

[java]  view plain copy
  1. <span style="font-size:18px;">1  
  2. 2  
  3. 3  
  4. 4  
  5. Exception in thread "main" java.lang.IllegalStateException  
  6.     at java.util.AbstractList$ListItr.set(AbstractList.java:408)  
  7.     at ljp.lists.ListDemo01.main(ListDemo01.java:20)</span>  

为什么会抛出IllegalStateException异常呢?IllegalStateException异常是指在不合理或不正确时间内唤醒一方法时出现的异常信息。换句话说,即 Java 环境或 Java 应用不满足请求操作。也就是说,set()方法不是这么使用的。

[java]  view plain copy
  1. <span style="font-size:18px;">package ljp.lists;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Collection;  
  6. import java.util.ListIterator;  
  7.   
  8. public class ListDemo01 {  
  9.     public static void main(String[] args) {  
  10.         List<Integer> arraylist = new ArrayList<Integer>();  
  11.         arraylist.add(1);  
  12.         arraylist.add(2);  
  13.         arraylist.add(3);  
  14.         arraylist.add(4);  
  15.         //查看arrayList中的内容  
  16.         for(int i : arraylist){  
  17.             System.out.println(i);  
  18.         }  
  19.         ListIterator<Integer> listit = arraylist.listIterator();  
  20.         while(listit.hasNext()){  
  21.             listit.next();  
  22.             listit.set(10);  
  23.         }  
  24.         //使用过set()方法之后,查看有什么变化  
  25.         for(int i : arraylist){  
  26.             System.out.println(i);  
  27.         }  
  28.     }  
  29. }  
  30. </span>  
[java]  view plain copy
  1.    

上面程序的运行结果为:

1

2

3

4

10

10

10

10

从结果可以看出,ListIterator接口中set()方法是将next()方法得到的元素重新赋值,当然,如果是previous()方法得到的元素也可以被赋值。
在List接口中,还有一个方法listIterator(int index)可以得到List的迭代器(ListIterator接口的对象),其中的参数index为迭代的起始位置,其它的同listIterator()方法得到的迭代器一样。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值