So this is just a dummy program to understand list Iterator. Steps that im doing
- created a ArrayList with "A" and "B"
- Now created a listIterator for this ArrayList
- If "B"is found ill add "C" next to it
- If "A" is found ill replace it with "a"
- If "B" is found ill replace it with "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.