Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Vector;
- import java.util.Scanner;
- class ShoppingItems{
- String name;
- }
- class vectors{
- public static void main(String[] args){
- Vector v = new Vector(5);
- ShoppingItems o = new ShoppingItems();
- int count = 0;
- while(true){
- System.out.print("\n1.Delete 2.Add at a position 3.Add at end 4.View the list 5.Exit: ");
- Scanner sc = new Scanner(System.in);
- switch(sc.nextInt()){
- case 1: System.out.print("Delete which index?: ");
- try{
- v.removeElementAt(sc.nextInt());
- count--;
- } catch(Exception e){
- System.out.print("Enter a whole number less than "+count);
- }
- break;
- case 2: System.out.print("Insert where and what?: ");
- try{
- v.add(sc.nextInt(), o.name = sc.next());
- count++;
- } catch(Exception e){
- System.out.print("Enter a whole number less than "+count);
- }
- break;
- case 3: System.out.print("Insert what?: ");
- v.add(o.name = sc.next());
- count++;
- break;
- case 4: if(count == 0) System.out.println("The list is empty.");
- else
- for(int i = 0; i< count; i++)
- System.out.println((i+1)+". "+v.elementAt(i));
- break;
- case 5: return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement