简体   繁体   English

列出Java的迭代器

[英]List Iterator of Java

So this is just a dummy program to understand list Iterator. 所以这只是一个理解列表迭代器的虚拟程序。 Steps that im doing 我正在做的步骤

  1. created a ArrayList with "A" and "B" 用“A”和“B”创建了一个ArrayList
  2. Now created a listIterator for this ArrayList 现在为此ArrayList创建了一个listIterator
  3. If "B"is found ill add "C" next to it 如果发现“B”,则在其旁边添加“C”
  4. If "A" is found ill replace it with "a" 如果发现“A”,则将其替换为“a”
  5. If "B" is found ill replace it with "b". 如果发现“B”,则将其替换为“b”。

Code: 码:

public class Main {
    public static void main(String[]  args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("A");
        al.add("B");

        ListIterator lItr = al.listIterator();
        while(lItr.hasNext()) {
        String s = (String)lItr.next();
        System.out.println(s);
        if(s.equals("B")) {
            lItr.add("C");
        }
        if(s.equals("A")) {
            lItr.set("a");
        }
        else if(s.equals("B")) {
            lItr.set("b");//Im getting an exception here saying
                            "java.lang.IllegalStateException"
        }
        }
        System.out.println(al);
    }
}

Please anyone tell why am i getting this exception why cant i set "B" to b. 请有人告诉我为什么我得到这个例外为什么我不能把“B”设置为b。

You are calling lItr.add("C") followed by lItr.set("b") without calling next or previous in between. 您正在调用lItr.add("C")然后调用lItr.add("C") lItr.set("b")而不调用其中的nextprevious

void java.util.ListIterator.set(Object e) void java.util.ListIterator.set(Object e)

Replaces the last element returned by next or previous with the specified element (optional operation). 用指定的元素替换next或previous返回的最后一个元素(可选操作)。 This call can be made only if neither remove nor add have been called after the last call to next or previous . 只有在最后一次调用next或previous之后才调用remove和add时,才能进行此调用

The documentation clearly says why this happens: 文档清楚地说明了为什么会这样:

Throws: 抛出:

UnsupportedOperationException - if the set operation is not supported by this list iterator UnsupportedOperationException - 如果此列表迭代器不支持set操作

ClassCastException - if the class of the specified element prevents it from being added to this list ClassCastException - 如果指定元素的类阻止将其添加到此列表中

IllegalArgumentException - if some aspect of the specified element prevents it from being added to this list IllegalArgumentException - 如果指定元素的某些方面阻止将其添加到此列表中

IllegalStateException - if neither next nor previous have been called, or remove or add have been called after the last call to next or previous IllegalStateException - 如果上次调用next或previous之前未调用next或previous,或者在上次调用next或previous之后调用了remove或add

You have called add before calling set , right? 你在调用set之前调用了add ,对吧?

if(s.equals("B")) {
    lItr.add("C"); // <-- here!
}
if(s.equals("A")) {
    lItr.set("a");
}
else if(s.equals("B")) {
    lItr.set("b"); // <-- and here
}

After you have added an element, the element you will set changes, so that is not allowed. 添加元素后,您将设置的元素发生更改,因此不允许。

To fix this, simply do the add after the set : 要解决此问题,只需在set后执行add

 // Also use generic types properly!
ListIterator<String> lItr = al.listIterator();
while(lItr.hasNext()) {
    String s = lItr.next();
    System.out.println(s);
    if(s.equals("A")) {
        lItr.set("a");
        lItr.add("C"); // note the change here
    }
    else if(s.equals("B")) {
        lItr.set("b");
    }
}

As per the Java documentation https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html#set-E- 根据Java文档https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html#set-E-

void set(E e) 空集(E e)

Replaces the last element returned by next() or previous() with the specified element (optional operation). 用指定的元素(可选操作)替换next()或previous()返回的最后一个元素。 This call can be made only if neither remove() nor add(E) have been called after the last call to next or previous. 只有在最后一次调用next或previous之后才调用remove()和add(E)时,才能进行此调用。

You are calling lItr.add("C") and then lItr.set("b") , with no call to next() or previous() in between, because both if conditions are checking for s.equals("B") and both of them will evaluate to true if the element is "B". 您正在调用lItr.add("C")然后lItr.set("b") ,而不调用next() or previous() ,因为两个条件都检查s.equals("B")和他们都将评估为true,如果该元素是“B”。

if(s.equals("B")) {
    lItr.add("C");
}

else if(s.equals("B")) {
    lItr.set("b");//Im getting an exception here saying
                    "java.lang.IllegalStateException"
}

This execution path occurs since you do not have an ELSE in the second IF condition, which makes the third IF condition run after the first IF is executed if element is "B". 由于在第二个IF条件下没有ELSE,因此如果元素为“B”,则在第一个IF执行后运行第三个IF条件,因此会执行此执行路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM