
whats the program to add,delete, display elements of an object using collecions. without using linked list

Hi Friend,
Try the following code:
import java.util.*;
class CollectionExample
{
public static void main(String[] args)
{
ArrayList list=new ArrayList();
Scanner input=new Scanner(System.in);
System.out.println("Enter List Elements: ");
for(int i=0;i<5;i++){
String st=input.next();
list.add(st);
}
System.out.println();
System.out.println("List Elements are: ");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println();
System.out.println("Enter the index of element to remove: ");
int index=input.nextInt();
list.remove(index);
System.out.println();
System.out.println("Now the list is: ");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println();
System.out.println("Enter the index of new element to add: ");
int in=input.nextInt();
list.add(in,"RoseIndia");
System.out.println();
System.out.println("Now the list is: ");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
For more information, visit the following links:
http://www.roseindia.net/javacodeexamples/index.shtml
http://www.roseindia.net/java/jdk6/
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.