Tuesday, 22 March 2016

ArrayList Demo 2

import java.util.*;
class ArrayListDemo
{  public static void main(String[] args)
   { ArrayList al=new ArrayList();
System.out.println("Initial size of al : "+ al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1,"A2");
al.add(new Integer(5));
System.out.println("Size of al after additions:"+al.size());
System.out.println("Contents of al:"+al.toString());
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions:"+al.size());
System.out.println("Contents of al:"+al);
  }
}

Thursday, 17 March 2016

ArrayList Demo 1

import java.util.*;
class ArrayListDemo1
{  public static void main(String[] args)
   { ArrayList<String> al=new ArrayList<String>();
System.out.println("Initial size of al : "+ al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1,"A2");
//al.add(new Integer(5)); //will not compile
System.out.println("Size of al after additions:"+al.size());
System.out.println("Contents of al:"+al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions:"+al.size());
System.out.println("Contents of al:"+al);
  }
}

Friday, 11 March 2016

ArrayList To Array conversation 2

import java.util.*;
class ArrayListToArray
{ public static void main(String[] args)
{ ArrayList al=new ArrayList();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
System.out.println("Contents of al : "+al);
System.out.println("Contents of al using iterator:");
Iterator itr = al.iterator();
while(itr.hasNext())
{
int x = (Integer)itr.next();
System.out.print(x + " ");
}
}
}

Thursday, 3 March 2016

ArrayList To Array conversion 1

import java.util.*;
class ArrayListToArray
{ public static void main(String[] args)
{ ArrayList al=new ArrayList();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
System.out.println("Contents of al : "+al);
System.out.println("Contents of al using iterator:");
Iterator itr = al.iterator();
while(itr.hasNext())
{
int x = (Integer)itr.next();
System.out.print(x + " ");
}
System.out.println();
Object ia[]= al.toArray();
int sum=0;
for(int i=0; i < ia.length; i++)
sum = sum + (Integer)ia[i];
System.out.println("Sum is "+sum);
}
}

Wednesday, 2 March 2016

Generic ArrayList

import java.util.*;
class GenericArrayList
{ public static void main(String[] args)
{ ArrayList<Integer> al=new ArrayList<Integer>();
al.add(1); //Auto Boxing
al.add(2); //Auto Boxing
al.add(3); //Auto Boxing
al.add(4); //Auto Boxing
System.out.println("Contents of al : "+al) ;
System.out.println("Iterating using for-each loop");
for(int i : al) //Auto Unboxing
{
System.out.print(i + " ");
}
}
}