Set size of Java Vector Example

  1. /*
  2.   Set size of Java Vector Example
  3.   This Java Example shows how to set the size of Java Vector object using
  4.   setSize method. Size of the Vector can be changed anytime after creating it using
  5.   this method.
  6. */
  7.  
  8. import java.util.Vector;
  9.  
  10. public class SetSizeOfJavaVectorExample {
  11.  
  12. public static void main(String[] args) {
  13. //create a Vector object
  14. Vector v = new Vector();
  15.  
  16. //Add elements to Vector
  17. v.add("1");
  18. v.add("2");
  19. v.add("3");
  20. v.add("4");
  21. v.add("5");
  22.  
  23. /*
  24.   To set the size of Vector use
  25.   void setSize(int newSize) method.
  26.   Please note that if the newSize is less than the current size of the Vector
  27.   elements at and after newSize index will be discarded, and if the newSize is
  28.   grater than the current size of Vector, null values are added at the end of
  29.   the Vector.
  30.   */
  31. //set size of Vector to 3. The Last 2 elements would be discarded.
  32. v.setSize(3);
  33.  
  34. //display elements of the Vector
  35. System.out.println("Vector elements after setting size to 3");
  36. for(int index=0; index < v.size(); index++)
  37. System.out.println(v.get(index));
  38.  
  39. //set size of Vector to 5. null values would be inserted in the Vector.
  40. v.setSize(5);
  41.  
  42. //display elements of the Vector
  43. System.out.println("Vector elements after setting size to 5");
  44. for(int index=0; index < v.size(); index++)
  45. System.out.println(v.get(index));
  46.  
  47. }
  48. }
  49. /*
  50. Output would be
  51. Vector elements after setting size to 3
  52. 1
  53. 2
  54. 3
  55. Vector elements after setting size to 5
  56. 1
  57. 2
  58. 3
  59. null
  60. null
  61. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!