fork download
  1.  
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class BasicVectorOperations {
  8.  
  9. public static void main(String a[]){
  10. Vector<String> v1 = new Vector<String>();
  11. //adding elements to the end
  12. v1.add("First");
  13. v1.add("Second");
  14. v1.add("Third");
  15. System.out.println(v1);
  16. //adding element at specified index
  17. v1.add(2,"Random");
  18. System.out.println(v1);
  19. //getting elements by index
  20. System.out.println("Element at index 3 is: "+v1.get(3));
  21. //getting first element
  22. System.out.println("The first element of this vector is: "+v1.firstElement());
  23. //getting last element
  24. System.out.println("The last element of this vector is: "+v1.lastElement());
  25. //how to check vector is empty or not
  26. System.out.println("Is this vector empty? "+v1.isEmpty());
  27. }
  28. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
[First, Second, Third]
[First, Second, Random, Third]
Element at index 3 is: Third
The first element of this vector is: First
The last element of this vector is: Third
Is this vector empty? false