Advertisement
jasonpogi1669

How to use java collections using Java (MP 9)

Mar 1st, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. /**
  9.  *
  10.  * @author markjasongalang
  11.  */
  12. import java.util.*;
  13.  
  14. public class Main {
  15.     public static void main(String[] args) {
  16.         System.out.println("Collections Menu: ");
  17.         System.out.println("[1] Linked List");
  18.         System.out.println("[2] Vector");
  19.         System.out.println("[3] Stack");
  20.         Scanner in = new Scanner(System.in);
  21.         int choice = in.nextInt();
  22.         switch (choice) {
  23.             case 1:
  24.                 System.out.println("Linked List: ");
  25.                 LinkedList<String> string_linked_list = new LinkedList<String>();
  26.                 string_linked_list.add("This is");
  27.                 string_linked_list.add("a linked");
  28.                 string_linked_list.add("list!");
  29.                 Iterator<String> it_linked_list = string_linked_list.iterator();
  30.                 while (it_linked_list.hasNext()) {
  31.                     System.out.print(it_linked_list.next() + " ");
  32.                 }
  33.                 System.out.println();
  34.                 break;
  35.             case 2:
  36.                 System.out.println("Vector: ");
  37.                 Vector<String> string_vector = new Vector<String>();
  38.                 string_vector.add("This is");
  39.                 string_vector.add("a vector");
  40.                 string_vector.add("!");
  41.                 Iterator<String> it_vector = string_vector.iterator();
  42.                 while (it_vector.hasNext()) {
  43.                     System.out.print(it_vector.next() + " ");
  44.                 }
  45.                 System.out.println();
  46.                 break;
  47.             case 3:
  48.                 System.out.println("Stack: ");
  49.                 Stack<String> string_stack = new Stack<String>();
  50.                 string_stack.push("This is");
  51.                 string_stack.push("a stack");
  52.                 string_stack.push("!");
  53.                 string_stack.pop();
  54.                 Iterator<String> it_stack = string_stack.iterator();
  55.                 while (it_stack.hasNext()) {
  56.                     System.out.print(it_stack.next() + " ");
  57.                 }
  58.                 System.out.println();
  59.                 break;
  60.             default:
  61.                 System.out.println("Invalid input.");
  62.         }
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement