My Code is as follows:
ArrayList<Integer> al = new ArrayList();
al.add(1);
al.add(2);
al.add(3);
al.add(3);
al.add(4);
I want the following pattern during printing:
[1,2,4]
I tried both for loop and Iterator but I am not getting the desired output.
Help me to sort out!
I found the solution and that is
for(int i=0;i<al.size();i++) {
if(al.get(i)!=3)
System.out.println(al.get(i));
}
thanks for your help
Now I want to get the same output, not through printing but by deleting elements of ArrayList, I tried with the same condition but I got exception
and the answer is
Iterator<Integer> iter = al.iterator();
while (iter.hasNext()) {
if (iter.next().intValue() == 3) {
iter.remove();
}
}
System.out.println(al);