{"id":729,"date":"2016-02-27T07:55:09","date_gmt":"2016-02-27T07:55:09","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=729"},"modified":"2016-02-27T07:55:09","modified_gmt":"2016-02-27T07:55:09","slug":"vector","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/vector","title":{"rendered":"Vector Tutorial In Java With Example"},"content":{"rendered":"<p>Vector is type of data structure that implements List Interface. It is very much similar to ArrayList as it also maintains insertion order, that is elements are retrieved in same order as they are added into it.<\/p>\n<p>Like Arrays it has index value that is automatically allocated to it, but main think which distinguishes it is, it can grow or shrink its capacity according to the need after it is created, as it implements growable array.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span><\/strong> Vector contains many legacy methods that are not part of collection framework which we will discuss\u00a0below.<\/p>\n<hr \/>\n<h4><strong>Example of Vector Class:<\/strong><\/h4>\n<p>Let us discuss Vector Class with the help of program. In this program we will create an object of Vector Class, some integer values are added and displayed.<\/p>\n<p>Following program has been divided into 3 Steps that we will discuss one by one:<\/p>\n<pre>import java.util.*;\r\npublic class VectorDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\/\/Step 1: Vector object is created\r\n  Vector&lt;Integer&gt; vectorObject = new Vector&lt;Integer&gt;(4);\u00a0\r\n \r\n\/\/Step 2: Integers are added\r\n  vectorObject.add(3);\u00a0 \r\n  vectorObject.add(5);\r\n  vectorObject.add(4);\r\n  vectorObject.add(1);\r\n  vectorObject.add(2);\r\n  vectorObject.add(8);\r\n  vectorObject.add(8);\r\n\r\n\/\/Step 3: Values are Displayed\r\n  System.out.println(\"Values in Vector Object\u00a0 :\" +vectorObject); \r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Values in Vector Object :[3, 5, 4, 1, 2, 8, 8]<\/pre>\n<p><strong>Explanation of Example:<\/strong><\/p>\n<ul>\n<li>In Step 1, we have created object of Vector class, we have defined these objects to store values of Integer type.<\/li>\n<li>In Step 2, we have used add method to store values in the data structure that we have created in step 1.<\/li>\n<li>In Step 3, we have displayed\u00a0values of these object.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Vector Methods In Java:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>\u00a01. boolean add(E e)<\/strong><\/span><\/p>\n<p>This method adds element at the end of the vector, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class VectorMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  Vector&lt;Integer&gt; vectorObject = new Vector&lt;Integer&gt;(4);\r\n\r\n  vectorObject.add(3);\r\n\r\n  vectorObject.add(5);\r\n\r\n  vectorObject.add(4);\r\n\r\n  vectorObject.add(1);\r\n\r\n  vectorObject.add(2);\r\n\r\n  vectorObject.add(8);\r\n\r\n  vectorObject.add(8);\r\n\r\n  System.out.println(\"Values in Vector Object\u00a0 :\" +vectorObject);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in Vector Object\u00a0 :[3, 5, 4, 1, 2, 8, 8]<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. void add(int index, E element)<\/strong><\/span><\/p>\n<p>This method adds an element at specified index and moves the whole elements one step in forward direction as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class VectorMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  Vector&lt;Integer&gt; vectorObject = new Vector&lt;Integer&gt;(4);\r\n\r\n  vectorObject.add(0,3);\r\n\r\n  vectorObject.add(1,5);\r\n\r\n  vectorObject.add(2,4);\r\n\r\n  vectorObject.add(3,1);\r\n\r\n  for(Integer integer: vectorObject)\r\n  {\r\n\r\n System.out.println(\"Index : \" +vectorObject.indexOf(integer)+ \" Value:  \"+integer);\r\n\r\n  }\r\n\r\n\/\/Adding element at index 2\r\n\r\n  vectorObject.add(2, 10);\r\n\r\n  System.out.println(\"\\nAfter adding value at index 2:\\n\");\r\n\r\n  for(Integer integer: vectorObject)\r\n\r\n  {   \r\n System.out.println(\"Index : \" +vectorObject.indexOf(integer)+ \" Value: \" +integer);\r\n\r\n  }\r\n }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Index : 0 Value: 3\r\n\r\nIndex : 1 Value: 5\r\n\r\nIndex : 2 Value: 4\r\n\r\nIndex : 3 Value: 1\r\n\r\nAfter adding value at index 2:\r\n\r\nIndex : 0 Value: 3\r\n\r\nIndex : 1 Value: 5\r\n\r\nIndex : 2 Value: 10\r\n\r\nIndex : 3 Value: 4\r\n\r\nIndex : 4 Value: 1<\/pre>\n<p><span style=\"color: #008000;\"><strong>\u00a03.\u00a0<\/strong><strong>void addElement(E obj)<\/strong><\/span><\/p>\n<p>This method adds the specified element at the end of the vector also increasing its size by 1, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class VectorMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  Vector&lt;Integer&gt; vectorObject = new Vector&lt;Integer&gt;(4);\r\n\r\n  vectorObject.add(3);\r\n\r\n  vectorObject.add(5);\r\n\r\n  vectorObject.add(4);\r\n\r\n  vectorObject.add(1);\r\n\r\n  System.out.println(\"Values in Vecor object\" +vectorObject);\r\n\r\n  vectorObject.addElement(2);\r\n\r\n  System.out.println(\"Values in Vecor object\" +vectorObject);\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in Vecor object[3, 5, 4, 1]\r\n\r\nValues in Vecor object[3, 5, 4, 1, 2]\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. int capacity()<\/strong><\/span><\/p>\n<p>This method gives the capacity of the vector, as shown in the following program:<\/p>\n<pre>import java.util.*;\r\n\r\npublic class VectorMethodsDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\r\n  Vector&lt;Integer&gt; vectorObject = new Vector&lt;Integer&gt;(4);\r\n\r\n  vectorObject.add(3);\r\n\r\n  vectorObject.add(5);\r\n\r\n  vectorObject.add(4);\r\n\r\n  vectorObject.add(1);\r\n\r\n  System.out.println(\"current capacity of Vector object is \" +vectorObject.capacity());\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>current capacity of Vector object is 4<\/pre>\n<p>Read All <a href=\"\/java\/vector-methods-example.html\">Vector Methods In JAVA With Example<\/a><\/p>\n<hr \/>\n<h4><strong>Important Points Of Vector:<\/strong><\/h4>\n<ul>\n<li>Vector is very much similar to ArrayList, but with few differences, firstly Vector is thread safe, where as Arraylist is not. Secondly, Vector is synchronized, where as Arraylist is not, So performance of Vector is low than ArrayList.<\/li>\n<li>Vector increases its size by 100 % that is it doubles its size when total number of elements exceeds its capacity whereas ArrayList increases by 50 % of current array size.<\/li>\n<li>Vector can use Enumeration interface as well as Iterator to traverse over its elements.<\/li>\n<li>Vector is a legacy Class, that is it is introduced before JDK 1.2.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Importance of Vectors:<\/strong><\/h4>\n<ul>\n<li>As vectors implements growable array. It grows and shrinks according to the need. So it is very helpful when we don\u2019t know requirement in advance, we can just declare a vector with some initial capacity, that automatically doubles its size when elements are added or removed.<\/li>\n<li>Vectors are thread safe, so it can be used in Multithreaded Environment.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Quick Revision Points Of Vector:<\/strong><\/h4>\n<ul>\n<li>Vector doubles its size when elements exceeds its initial capacity.<\/li>\n<li>Vector can use both Enumeration as well as Iterator to traverse elements.<\/li>\n<li>Vector is\u00a0synchronized.<\/li>\n<li>Vector is\u00a0Thread Safe.<\/li>\n<li>Vectors can be used for Multi Threading.<\/li>\n<li>Vector is slow as compared to Array List.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Vector is type of data structure that implements List Interface. It is very much similar to ArrayList as it also maintains insertion order, that is elements are retrieved in same order as they are added into it. Like Arrays it has index value that is automatically allocated to it, but main think which distinguishes it &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/vector\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Vector Tutorial In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-729","page","type-page","status-publish","hentry"],"psp_head":"<title>Vector Class Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on Vector Class which is group of elements that implements List Interface with example and code in JAVA. Also find details of Vectors methods, implementation and its importance.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/vector\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/729\/revisions"}],"predecessor-version":[{"id":1501,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/729\/revisions\/1501"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}