import java.util.ArrayList;
import java.util.Iterator;
public class ArrayDeletion {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
ArrayList<String> list1=new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
ArrayList<String> list2=new ArrayList<String>();
list2.add("a");
list2.add("b");
list2.add("c");
list2.add("d");
ArrayList<String> list3=new ArrayList<String>();
list3.add("a");
list3.add("b");
list3.add("c");
list3.add("d");
System.out.println("Elements of list:"+list);
System.out.println("Elements of list1:"+list1);
System.out.println("Elements of list2:"+list2);
//Method 1 which is used by many which gives the wrong result
for(int i=0;i<list.size();i++)
{
System.out.println("Size:"+list.size());
list.remove(i);
}
System.out.println("After deletion list elements are "+list);
//Method 2 which gives right output and simplified form of method 1
//don't use i<list1.size() in for loop otherwise there will be an
//ArrayIndexOutOfBoundException
//because size of ArrayList will be decreased after deletion
//So it is advised to use a variable which stores the size of list and pass this variable to
// for loop
int length=list1.size();
for(int i=0;i<length;i++)
{
list1.remove(0);
}
System.out.println("After deletion list1 elements are "+list1);
//Method 3 using Iterator Class
Iterator<String> itr=list2.iterator();
while(itr.hasNext())
{
String element=itr.next();
itr.remove();
}
System.out.println("After deletion list2 elements are "+list2);
//Method 4 using foreach loop
//by using foreach loop we will get Exception i.e. ConcurrentModificaionException
//this is because for deleting elemens from arraylist
//we have to first use next() and then use remove() as done in method 3
//but in foreach loop compiler will first remove the element
//and then call the next() function
//so there will be this exception
for(String s:list3)
{
list3.remove(s);
}
System.out.println("After deletion list3 elements are "+list3);
}
}
import java.util.Iterator;
public class ArrayDeletion {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
ArrayList<String> list1=new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
ArrayList<String> list2=new ArrayList<String>();
list2.add("a");
list2.add("b");
list2.add("c");
list2.add("d");
ArrayList<String> list3=new ArrayList<String>();
list3.add("a");
list3.add("b");
list3.add("c");
list3.add("d");
System.out.println("Elements of list:"+list);
System.out.println("Elements of list1:"+list1);
System.out.println("Elements of list2:"+list2);
//Method 1 which is used by many which gives the wrong result
for(int i=0;i<list.size();i++)
{
System.out.println("Size:"+list.size());
list.remove(i);
}
System.out.println("After deletion list elements are "+list);
//Method 2 which gives right output and simplified form of method 1
//don't use i<list1.size() in for loop otherwise there will be an
//ArrayIndexOutOfBoundException
//because size of ArrayList will be decreased after deletion
//So it is advised to use a variable which stores the size of list and pass this variable to
// for loop
int length=list1.size();
for(int i=0;i<length;i++)
{
list1.remove(0);
}
System.out.println("After deletion list1 elements are "+list1);
//Method 3 using Iterator Class
Iterator<String> itr=list2.iterator();
while(itr.hasNext())
{
String element=itr.next();
itr.remove();
}
System.out.println("After deletion list2 elements are "+list2);
//Method 4 using foreach loop
//by using foreach loop we will get Exception i.e. ConcurrentModificaionException
//this is because for deleting elemens from arraylist
//we have to first use next() and then use remove() as done in method 3
//but in foreach loop compiler will first remove the element
//and then call the next() function
//so there will be this exception
for(String s:list3)
{
list3.remove(s);
}
System.out.println("After deletion list3 elements are "+list3);
}
}
0 comments:
Post a Comment