Java Tip : How to avoid an java.util.ConcurrentModificationException with ArrayList?

You need to add/delete an item in an ArrayList when you are iterating the list. You will receive the java.util.ConcurrentModificationException exception. For example, the following code will throw an exception after adding an item into list:

public class Sample {

  public static void main(String[] args) {

    List<Integer>  iList = new ArrayList<Integer>();
    for (int i = 0; i != 100; i++)
      iList.add(i);

    int addValue = 1000;
    for (Integer i: iList) {
      if (i%10 == 0) {
        iList.add(addValue++);
      }
    }
}

To avoid java.util.ConcurrentModificationException exception, we can add an item through the iterator of list. If we do the same as the above code, the next access item in list via the iterator will generate the same exception.

public class Sample {

  public static void main(String[] args) {

    List<Integer>  iList = new ArrayList<Integer>();
    for (int i = 0; i != 100; i++)
      iList.add(i);

    int addValue = 1000;

    for (ListIterator<Integer> itr = iList.listIterator(); itr.hasNext();) {
      Integer i = itr.next();
      if (i%10 == 0) {
        itr.add(addValue++);
      }
    }

}

JavaTip : final values can be modified but cannot be reassigned

if I had a Person object and did final Person p = new Person("me"); it means I can’t reassignp to point to another Person object. I can still do p.setFirstName("you");

What confuses the situation is that

final int PI = 3.14;
final String greeting = "Hello World!";

Containers or objects with mutator methods that can alter the internal state of the object are not const just the reference to those objects are final and can’t be reassigned to reference another object.

Can a programmer work without the if statement ?

All the programming is nothing but usage of loops and conditional statements. Entire programming is mainly based on
1. If statements
2. For loops

If you know these you can easily do programming. A good programmer is one who uses these things efficiently to reduce execution time with better readability and maintainable code. Can a programmer work without the if (conditional statements) ?
It is difficult but entire programming can be done with only For loop construct.

How ? well inside the for loop there is conditional statement which could be used wisely to avoid using if.

for ( initialization; conditional statement; update){
do some thing
}

String sex = “male”;
if(sex.equalsIgnoreCase(“male”)) {
System.out.println(“Man is the king “);
} else{
System.out.println(“WoMan is the Queen “);
}

lets say we need to convert this if structure in for loop.
            for(;sex.equalsIgnoreCase(“male”);){
                System.out.println(“Man is the king “);
            }
In first attempt it will be some like this but this will cause the infinite loop so we must have to put break after our job is over to break the loop.
            for(;sex.equalsIgnoreCase(“male”);){
                System.out.println(“Man is the king “);
                break;
            }

Now how to use the else  part of the if structure.
for(;!sex.equalsIgnoreCase(“male”);){
System.out.println(“WoMan is the Queen “);
break;
}