{"id":1257,"date":"2022-02-26T07:01:47","date_gmt":"2022-02-26T07:01:47","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1257"},"modified":"2022-02-26T07:01:47","modified_gmt":"2022-02-26T07:01:47","slug":"java-vector","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-vector\/","title":{"rendered":"Java Vector"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Vector class and how to use it. We will also learn how it is different from the ArrayList class, and why we should use array lists instead.<\/p>\n\n\n\n<p>The\u00a0<code>Vector<\/code>\u00a0class is an implementation of the\u00a0<code>List<\/code>\u00a0interface that allows us to create resizable-arrays similar to the\u00a0ArrayList\u00a0class.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"vs-arraylist\">Java Vector vs. ArrayList<\/h2>\n\n\n\n<p>In Java, both&nbsp;<code>ArrayList<\/code>&nbsp;and&nbsp;<code>Vector<\/code>&nbsp;implements the&nbsp;<code>List<\/code>&nbsp;interface and provides the same functionalities. However, there exist some differences between them.<\/p>\n\n\n\n<p>The&nbsp;<code>Vector<\/code>&nbsp;class synchronizes each individual operation. This means whenever we want to perform some operation on vectors, the&nbsp;<code>Vector<\/code>&nbsp;class automatically applies a lock to that operation.<\/p>\n\n\n\n<p>It is because when one thread is accessing a vector, and at the same time another thread tries to access it, an exception called&nbsp;<code>ConcurrentModificationException<\/code>&nbsp;is generated. Hence, this continuous use of lock for each operation makes vectors less efficient.<\/p>\n\n\n\n<p>However, in array lists, methods are not synchronized. Instead, it uses the&nbsp;<code>Collections.synchronizedList()<\/code>&nbsp;method that synchronizes the list as a whole.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;It is recommended to use&nbsp;<code>ArrayList<\/code>&nbsp;in place of&nbsp;<code>Vector<\/code>&nbsp;because vectors&nbsp;less efficient.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Creating a Vector<\/h2>\n\n\n\n<p>Here is how we can create vectors in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Vector&lt;Type&gt; vector = new Vector&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>Type<\/var>&nbsp;indicates the type of a linked list. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create Integer type linked list\nVector&lt;Integer&gt; vector= new Vector&lt;&gt;();\n\n\/\/ create String type linked list\nVector&lt;String&gt; vector= new Vector&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of Vector<\/h2>\n\n\n\n<p>The&nbsp;<code>Vector<\/code>&nbsp;class also provides the resizable-array implementations of the&nbsp;<code>List<\/code>&nbsp;interface (similar to the&nbsp;<code>ArrayList<\/code>&nbsp;class). Some of the&nbsp;<code>Vector<\/code>&nbsp;methods are:<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"add-elements\">Add Elements to Vector<\/h2>\n\n\n\n<ul><li><code>add(element)<\/code>&nbsp;&#8211; adds an element to vectors<\/li><li><code>add(index, element)<\/code>&nbsp;&#8211; adds an element to the specified position<\/li><li><code>addAll(vector)<\/code>&nbsp;&#8211; adds all elements of a vector to another vector<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Vector;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Vector&lt;String&gt; mammals= new Vector&lt;&gt;();\n\n        \/\/ Using the add() method\n        mammals.add(\"Dog\");\n        mammals.add(\"Horse\");\n\n        \/\/ Using index number\n        mammals.add(2, \"Cat\");\n        System.out.println(\"Vector: \" + mammals);\n\n        \/\/ Using addAll()\n        Vector&lt;String&gt; animals = new Vector&lt;&gt;();\n        animals.add(\"Crocodile\");\n\n        animals.addAll(mammals);\n        System.out.println(\"New Vector: \" + animals);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Vector: &#91;Dog, Horse, Cat]\nNew Vector: &#91;Crocodile, Dog, Horse, Cat]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access\">Access Vector Elements<\/h2>\n\n\n\n<ul><li><code>get(index)<\/code>&nbsp;&#8211; returns an element specified by the index<\/li><li><code>iterator()<\/code>&nbsp;&#8211; returns an iterator object to sequentially access vector elements<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Iterator;\nimport java.util.Vector;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Vector&lt;String&gt; animals= new Vector&lt;&gt;();\n        animals.add(\"Dog\");\n        animals.add(\"Horse\");\n        animals.add(\"Cat\");\n\n        \/\/ Using get()\n        String element = animals.get(2);\n        System.out.println(\"Element at index 2: \" + element);\n\n        \/\/ Using iterator()\n        Iterator&lt;String&gt; iterate = animals.iterator();\n        System.out.print(\"Vector: \");\n        while(iterate.hasNext()) {\n            System.out.print(iterate.next());\n            System.out.print(\", \");\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Element at index 2: Cat\nVector: Dog, Horse, Cat,\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"remove\">Remove Vector Elements<\/h2>\n\n\n\n<ul><li><code>remove(index)<\/code>&nbsp;&#8211; removes an element from specified position<\/li><li><code>removeAll()<\/code>&nbsp;&#8211; removes all the elements<\/li><li><code>clear()<\/code>&nbsp;&#8211; removes all elements. It is more efficient than&nbsp;<code>removeAll()<\/code><\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Vector;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        Vector&lt;String&gt; animals= new Vector&lt;&gt;();\n        animals.add(\"Dog\");\n        animals.add(\"Horse\");\n        animals.add(\"Cat\");\n\n        System.out.println(\"Initial Vector: \" + animals);\n\n        \/\/ Using remove()\n        String element = animals.remove(1);\n        System.out.println(\"Removed Element: \" + element);\n        System.out.println(\"New Vector: \" + animals);\n\n        \/\/ Using clear()\n        animals.clear();\n        System.out.println(\"Vector after clear(): \" + animals);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Initial Vector: &#91;Dog, Horse, Cat]\nRemoved Element: Horse\nNew Vector: &#91;Dog, Cat]\nVector after clear(): &#91;]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"other-methods\">Others Vector Methods<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Descriptions<\/th><\/tr><tr><td><code>set()<\/code><\/td><td>changes an element of the vector<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>returns the size of the vector<\/td><\/tr><tr><td><code>toArray()<\/code><\/td><td>converts the vector into an array<\/td><\/tr><tr><td><code>toString()<\/code><\/td><td>converts the vector into a String<\/td><\/tr><tr><td><code>contains()<\/code><\/td><td>searches the vector for specified element and returns a boolean result<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Vector class and how to use it. We will also learn how it is different from the ArrayList class, and why we should use array lists instead. The\u00a0Vector\u00a0class is an implementation of the\u00a0List\u00a0interface that allows us to create resizable-arrays similar to the\u00a0ArrayList\u00a0class. Java Vector vs. ArrayList In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[584],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1257"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1257"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1257\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}