Advertisement
Shailrshah

Element Insertion & Deletion in a Vector

Sep 20th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. /*Write a menu-driven progroam to insert and delete various objects in a vector of the specified size.*/
  2. import java.util.Vector;
  3. import java.util.Scanner;
  4. class Employee{
  5.     int id;
  6. }
  7. class Student{
  8.     int roll_no;
  9. }
  10. class Practice{
  11.     public static void main(String[] args){
  12.         Vector v = new Vector(20);
  13.         Scanner sc = new Scanner(System.in);
  14.         while(true){
  15.             System.out.print("\nThe vector is ");
  16.             if(v.size() == 0) System.out.print("Empty!");
  17.             else for(int i = 0; i< v.size(); i++)
  18.                     System.out.print(" " + v.elementAt(i)+ " ");
  19.             System.out.println("\n\n1. Insert 2. Delete 3. Exit");
  20.             int choice = sc.nextInt();
  21.             switch(choice){
  22.                 case 1: System.out.println("Insert a 1. Integer 2. Double 3. Float 4. String 5. Array of Strings 6. Employee Object 7. Student Object.");
  23.                     int type = sc.nextInt();
  24.                     switch(type){
  25.                         case 1: System.out.print("Enter the integer: ");
  26.                             v.addElement(new Integer(sc.nextInt())); break;
  27.                         case 2: System.out.print("Enter the double: ");
  28.                         v.addElement(new Double(sc.nextDouble())); break;
  29.                         case 3: System.out.print("Enter the float: ");
  30.                             v.addElement(new Float(sc.nextFloat())); break;
  31.                         case 4: System.out.print("Enter the String: ");
  32.                             v.addElement(sc.next()); break;
  33.                         case 5: System.out.print("Enter no. of strings: ");
  34.                             String[] string = new String[sc.nextInt()];
  35.                             for(String i : string) i = sc.next();
  36.                             for(String i : string) v.addElement(i); break;
  37.                         case 6: System.out.print("Enter the ID of employee: ");
  38.                             Employee em = new Employee();
  39.                             v.addElement(em.id = sc.nextInt()); break;
  40.                         case 7: System.out.print("Enter the roll no: ");
  41.                             Student st = new Student();
  42.                             v.addElement(st.roll_no = sc.nextInt()); break;
  43.                         default:System.out.println("Invalid choice!");
  44.                         } break;
  45.                 case 2: System.out.println("Enter the element's index: ");
  46.                     v.removeElementAt(sc.nextInt()); break;
  47.                 case 3: return;
  48.                 default: System.out.println("Wrong Choice!");
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement