Need some clarification about java.util.list. I am using eclipse for development.
I wrote this code
public static void main(String[] asdf){
List<Integer> lst = new ArrayList<Integer>();
for(int i=0;i<10000;i++){
lst.add(i);
}
System.out.println(lst.size());
for(int i=0;i<10000;i++){
if((i%50)==0){
lst.remove(i);
}
}
System.out.println(lst.size());
}
But when i run this code it gives exception
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9850, Size: 9803
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.remove(ArrayList.java:445)
at com.ilex.reports.action.rpt.CNSReports.main(CNSReports.java:301)
one more this to note is

Then i did one change in code i-e iterated 2nd loop till 5000 only and it worked fine

Questions are
Why giving IndexOutOfBoundsException ?
What is that modCoutn ?
Do any of this thing become reason for memory Leak if yes how to resolve it ?
Thanks in advance.
Removing elements from a list makes it smaller. Your second loop runs until 10000, but the list will have shrunk to less than 10000 by the time it gets there.
In fact, if your intent is to remove all multiples of 50, you could loop backwards from 10000 to 0, with a step size of 50 and avoid this problem, and be faster.
Note that your current approach, if your intent is to remove the multiples of 50, won’t work, since after the first remove the invariant that the value at each index is the index, no longer holds.
modCount is an internal variable that ArrayList uses to detect whether it is changed in reference to any Iterators over it. It basically counts all modifications to the List. An Iterator keeps its own count, and checks whether it remains in sync with the List.
Your code is not causing any memory to leak. In java memory ‘leaks’ if Objects that are no longer used are still referenced so they cannot be garbage collected. But since everything in the example is passed outside the scope of the method, everything can be gc’d once the method is left. (and since it is the main method, the vm will stop running and release its memory too)