{"id":163,"date":"2019-01-24T18:22:39","date_gmt":"2019-01-24T18:22:39","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=163"},"modified":"2020-08-06T10:11:11","modified_gmt":"2020-08-06T10:11:11","slug":"27-java-vectors","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/","title":{"rendered":"Java &#8211; Vectors"},"content":{"rendered":"<p>We have already discussed ArrayLists and\u00a0 LinkedList. We also discussed difference between ArrayList and Lists in Java.<\/p>\n<p>I suggest you review them:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/java\/java-arraylist-and-linkedlist\/\">ArrayList and LinkedList in Java<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/difference-between-list-and-arraylist-in-java\/\">Difference Between List and ArrayList in Java<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>However, today, we would learn about Vectors. You use the Vector class to implement a dynamic array in Java. Similar to ArrayLists, Vectors also derives from the List class.<\/p>\n<p>But unlike ArrayLists:<\/p>\n<ul>\n<li>Vector contains additional methods not in collection framework<\/li>\n<li>Vectors are synchronised (they are thread-safe)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>How to Create a vector<\/strong><\/p>\n<p>Similar to how you create an ArrayList, you can also create a vector using its constructor. Example is given below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">\tVector vector1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Vector<span style=\"color: #333333;\">();<\/span>\r\n\tVector<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> vector2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Vector<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;();<\/span>\t\t\r\n<\/pre>\n<p>The first line creates a vector of objects while the second line creates a vector of Strings.<\/p>\n<p>&nbsp;<\/p>\n<p>You can as well use any of the constructor listed below<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Constructor and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1.<\/td>\n<td><b>Vector( )<\/b><\/p>\n<p>Creates a vector of initial size of 10. The default data type is object. We already used this<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>Vector(int size)<\/b><\/p>\n<p>Accepts a parameter of the\u00a0 initial size of the vector . Then creates a vector whose initial capacity is the given size<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>Vector(int size, int incr)<\/b><\/p>\n<p>Creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies number of elements to allocate any time the\u00a0 vector is resized upward.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>Vector(Collection c)<\/b><\/p>\n<p>This constructor creates a vector from\u00a0 the elements of collection c. So the elements of the collection would be transferred to the new vector.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>The program below creates a vector of Strings, vector2.<br \/>\nThen uses the add() method to add 3 strings to the vector<br \/>\nFinally uses an enhanced for loop to print out the elements of the vector<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Vector<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">VectorDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\tVector<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> vector2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Vector<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;();<\/span>\t\r\n\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Saffon\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Oleander\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>String element <span style=\"color: #333333;\">:<\/span>vector2<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>element<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n\t\r\n<\/pre>\n<p>I would then provide a list of methods available in a vector class. You can therefore use these methods to perform any kind of operations on a vector.<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN.<\/th>\n<th>Method and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>add(int index, Object element)<\/b><\/p>\n<p>Inserts the specified element at the position(given by index) in this Vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>add(Object o)<\/b><\/p>\n<p>Adds an\u00a0 element to the end of the Vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>boolean addAll(Collection c)<\/b><\/p>\n<p>Copies all of the elements in the given Collection to the end of this Vector, in the order that they are returned by the given Collection&#8217;s Iterator.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>addAll(int index, Collection c)<\/b><\/p>\n<p>Copies all of the elements in in the given Collection c, into the Vector at the specified index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>addElement(Object obj)<\/b><\/p>\n<p>Adds the specified component to the end of this vector, increasing its size of the vector by one.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>int capacity()<\/b><\/p>\n<p>You use this to get the capacity of the Vector<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>clear()<\/b><\/p>\n<p>Deletes all of the elements of the vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><b>clone()<\/b><\/p>\n<p>Returns a clone of the vector as an object<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><b>contains(Object elem)<\/b><\/p>\n<p>Checks if the given object is an element in the vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><b>containsAll(Collection c)<\/b><\/p>\n<p>Checks if the vector contains all of the elements in the given Collection. Returns true if yes. Otherwise, returns false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">11<\/td>\n<td><b>copyInto(Object[] anArray)<\/b><\/p>\n<p>Copies the elements of this vector into the given array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">12<\/td>\n<td><b>elementAt(int index)<\/b><\/p>\n<p>Returns the element at the given index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">13<\/td>\n<td><b>Enumeration elements()<\/b><\/p>\n<p>Used to returns an enumeration of the elements of the vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">14<\/td>\n<td><b>ensureCapacity(int minCapacity)<\/b><\/p>\n<p>Used to increase the capacity of the vector to the capacity specified by minCapacity<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">15<\/td>\n<td><b>equals(Object o)<\/b><\/p>\n<p>Uses to check is the vector and the spefied object are equal<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">16<\/td>\n<td><b>firstElement()<\/b><\/p>\n<p>Used to return the first component (the item at index 0) of this vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">17<\/td>\n<td><b>get(int index)<\/b><\/p>\n<p>Used to return the element at the specified position in this vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">18<\/td>\n<td><b>hashCode()<\/b><\/p>\n<p>Used to return the hash code value for this vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">19<\/td>\n<td><b>indexOf(Object elem)<\/b><\/p>\n<p>Searches for the first occurrence of the specified parameter, checking for equality using the equals method.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">20<\/td>\n<td><b>indexOf(Object elem, int index)<\/b><\/p>\n<p>Searches for the first occurrence of the specified parameter, beginning at index, and checking for equality using the equals method.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">21<\/td>\n<td><b>insertElementAt(Object obj, int index)<\/b><\/p>\n<p>Inserts the given object as a component in this vector at the given index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">22<\/td>\n<td><b>isEmpty()<\/b><\/p>\n<p>Checks if this vector is empty. Returns true if the vector is empty. Returns false if othewise<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">23<\/td>\n<td><b>lastElement()<\/b><\/p>\n<p>Used to return to return\u00a0 the last element of the vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">24<\/td>\n<td><b>lastIndexOf(Object elem)<\/b><\/p>\n<p>Used to return the index of the last occurrence of the specified object in the vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">25<\/td>\n<td><b>lastIndexOf(Object elem, int index)<\/b><\/p>\n<p>Searches backwards for the given object, beginning at the specified index, and returns an index the element.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">26<\/td>\n<td><b>remove(int index)<\/b><\/p>\n<p>Removes the particular element at the specified index in the vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">27<\/td>\n<td><b>remove(Object o)<\/b><\/p>\n<p>Removes the first occurrence of the given element from\u00a0 the vector, If the vector does not contain the element, it is unchanged.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">28<\/td>\n<td><b>removeAll(Collection c)<\/b><\/p>\n<p>Removes from this vector all of its elements that are contained in the given Collection<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">29<\/td>\n<td><b>removeAllElements()<\/b><\/p>\n<p>Takes out all of the\u00a0 elements\u00a0 of this vector and sets its size to zero.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">30<\/td>\n<td><b>removeElement(Object obj)<\/b><\/p>\n<p>Removes the first occurrence of the specified\u00a0 argument from this vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">31<\/td>\n<td><b>removeElementAt(int index)<\/b><\/p>\n<p>Removes element at the given index from the Vector<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">32<\/td>\n<td><b>removeRange(int fromIndex, int toIndex)<\/b><\/p>\n<p>Removes all of the elements in the vector\u00a0 from fromIndex, inclusive and toIndex, exclusive.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">33<\/td>\n<td><b>retainAll(Collection c)<\/b><\/p>\n<p>Retains only the elements in this vector that are contained in the given Collection.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">34<\/td>\n<td><b>set(int index, Object element)<\/b><\/p>\n<p>Use this to replace the element at the specified position in the vector with the specified element.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">35<\/td>\n<td><b>setElementAt(Object obj, int index)<\/b><\/p>\n<p>Sets the element at the specified index of the vector to be the given object.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">36<\/td>\n<td><b>setSize(int newSize)<\/b><\/p>\n<p>Sets the size of the vector to the newSize<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">37<\/td>\n<td><b>int size()<\/b><\/p>\n<p>Used to return\u00a0 the number of components in this vector.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">38<\/td>\n<td><b>subList(int fromIndex, int toIndex)<\/b><\/p>\n<p>Used to return\u00a0 a\u00a0 subList of the vector between fromIndex, inclusive, and toIndex, exclusive.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">39<\/td>\n<td><b>Object[] toArray()<\/b><\/p>\n<p>Used to return\u00a0 an object array containing all of the items in the vector in the correct order.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">40<\/td>\n<td><b>Object[] toArray(Object[] a)<\/b><\/p>\n<p>Used to return an object array containing all of the item in the vector in the correct order; the runtime type of the returned array is that of the given array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">41<\/td>\n<td><b>String toString()<\/b><\/p>\n<p>Used to return\u00a0 a string representation of the vector, containing the String representation of each of the element.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">42<\/td>\n<td><b>void trimToSize()<\/b><\/p>\n<p>Reduces the capacity of this vector down to\u00a0 the vector&#8217;s current size.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.1: Methods in the Vector Class<\/p>\n<p>&nbsp;<\/p>\n<p>Below is a program that illustrates how to use some of the methods listed above. I recommend you run this code yourself. Also try\u00a0 to use other methods to see how they work.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.ArrayList<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Vector<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">VectorDemo2<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t\tVector<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> vector2 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Vector<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;();<\/span>\t\r\n\t\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Saffon\"<\/span><span style=\"color: #333333;\">);<\/span>\t\t\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>String element <span style=\"color: #333333;\">:<\/span>vector2<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>element<span style=\"color: #333333;\">);<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Current Size: \"<\/span> <span style=\"color: #333333;\">+<\/span> vector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">size<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Current Capacity: \"<\/span> <span style=\"color: #333333;\">+<\/span> vector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">capacity<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\t\r\n\t\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ensureCapacity<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">25<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New Size: \"<\/span> <span style=\"color: #333333;\">+<\/span> vector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">size<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"New Capacity: \"<\/span> <span style=\"color: #333333;\">+<\/span> vector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">capacity<\/span><span style=\"color: #333333;\">());<\/span>\t\t\r\n\t\t\r\n\t\tvector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addElement<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Oleander\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>vector2<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isEmpty<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Program to demonstrate use or Vector Methods<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already discussed ArrayLists and\u00a0 LinkedList. We also discussed difference between ArrayList and Lists in Java. I suggest you review them: ArrayList and LinkedList &hellip; <\/p>\n","protected":false},"author":395,"featured_media":165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Vectors - Java Tutorials<\/title>\n<meta name=\"description\" content=\"Learn about the vector class in Java. You&#039;ll learn how to create a vector using the constructors as well as how to use various methods\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java - Vectors - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"Learn about the vector class in Java. You&#039;ll learn how to create a vector using the constructors as well as how to use various methods\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-24T18:22:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-06T10:11:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Vectors-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"950\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Vectors\",\"datePublished\":\"2019-01-24T18:22:39+00:00\",\"dateModified\":\"2020-08-06T10:11:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/\"},\"wordCount\":1093,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Vectors-in-Java.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/\",\"name\":\"Java - Vectors - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Vectors-in-Java.jpg\",\"datePublished\":\"2019-01-24T18:22:39+00:00\",\"dateModified\":\"2020-08-06T10:11:11+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"Learn about the vector class in Java. You'll learn how to create a vector using the constructors as well as how to use various methods\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Vectors-in-Java.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Vectors-in-Java.jpg\",\"width\":950,\"height\":518,\"caption\":\"Vectors in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/27-java-vectors\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Vectors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Vectors - Java Tutorials","description":"Learn about the vector class in Java. You'll learn how to create a vector using the constructors as well as how to use various methods","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/","og_locale":"en_US","og_type":"article","og_title":"Java - Vectors - Java Tutorials","og_description":"Learn about the vector class in Java. You'll learn how to create a vector using the constructors as well as how to use various methods","og_url":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-24T18:22:39+00:00","article_modified_time":"2020-08-06T10:11:11+00:00","og_image":[{"width":950,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Vectors-in-Java.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Vectors","datePublished":"2019-01-24T18:22:39+00:00","dateModified":"2020-08-06T10:11:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/"},"wordCount":1093,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Vectors-in-Java.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/","url":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/","name":"Java - Vectors - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Vectors-in-Java.jpg","datePublished":"2019-01-24T18:22:39+00:00","dateModified":"2020-08-06T10:11:11+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"Learn about the vector class in Java. You'll learn how to create a vector using the constructors as well as how to use various methods","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Vectors-in-Java.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Vectors-in-Java.jpg","width":950,"height":518,"caption":"Vectors in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/27-java-vectors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Vectors"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":7,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/163\/revisions\/214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/165"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}