/*create an array using ArrayList class and using size(), add(), remove() method
insert an elements in array and remove elements get the size of array
and finally print the array to the screen.*/
import java.util.*;
class Aray
{
public static void main(String[] args)
{
String s="This";
int c=1,d=1;
ArrayList a=new ArrayList();
System.out.println("THe Current SiZe : "+a.size());
System.out.println("\nAll 10 elements : \n");
for(int i=0;i<10;i++)
{
d=d*2;
a.add(i,d);
}
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("\nNow Removing Botton 5 elements : \n");
try{
for(int i=5;i<10;i++)
a.remove(i);
}
catch(IndexOutOfBoundsException ab)
{
}
a.trimToSize();
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<5;i++)
System.out.println(a.get(i)+"\n");
for(int i=5;i<10;i++)
{
a.add(i,s);
s=s+" is";
}
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("The Index of 32 : "+a.indexOf(32));
System.out.println("THe Current SiZe : "+a.size());
}
}
insert an elements in array and remove elements get the size of array
and finally print the array to the screen.*/
import java.util.*;
class Aray
{
public static void main(String[] args)
{
String s="This";
int c=1,d=1;
ArrayList a=new ArrayList();
System.out.println("THe Current SiZe : "+a.size());
System.out.println("\nAll 10 elements : \n");
for(int i=0;i<10;i++)
{
d=d*2;
a.add(i,d);
}
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("\nNow Removing Botton 5 elements : \n");
try{
for(int i=5;i<10;i++)
a.remove(i);
}
catch(IndexOutOfBoundsException ab)
{
}
a.trimToSize();
System.out.println("THe Current SiZe : "+a.size());
for(int i=0;i<5;i++)
System.out.println(a.get(i)+"\n");
for(int i=5;i<10;i++)
{
a.add(i,s);
s=s+" is";
}
for(int i=0;i<10;i++)
System.out.println(a.get(i)+"\n");
System.out.println("The Index of 32 : "+a.indexOf(32));
System.out.println("THe Current SiZe : "+a.size());
}
}
No comments:
Post a Comment