![]() |
[JAVA] A Guide to Using Vectors - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE) +--- Thread: [JAVA] A Guide to Using Vectors (/Thread-JAVA-A-Guide-to-Using-Vectors) |
[JAVA] A Guide to Using Vectors - Solixious - 11-18-2012 Java vectors have a big advantage over the array data structure since it is very flexible and its size can be increased or decreased very easily. Importing Vector Vectors are a part of java.util package so you need to import it before using it. Code: import java.util.Vector; Creating a new Vector One following constructors are to be used to create a Vector. Code: public Vector() Example : Code: Vector v=new Vector(5,3); This will create a new Vector that has a size of 5 i.e. it can store 5 pieces of information. If you try to store a sixth piece, it will automatically re-size to 8 (5+3). If you add nine, it'll re-size to 12, and so on. Adding Elements to a Vector The two basic ways of adding elements to a vector is by using the following methods Code: public boolean add (Object o) The first function appends the element to the end of the vector while the second element adds the element at specific position while resizing the Vector and shifting the remaining elements. Example : Code: v.add("Hack Community"); Removing Elements from a Vector Removing element(s) from a Vector is very simple. The methods to be used are as follows : Code: public Object remove(int index) The above method will remove the element from the specified index and return the value of the element that was removed. After removing the element, it will shift the remaining elements to fill up the voids. Code: public void remove(Object element) The above method will search the entire vector for the element passed as argument, remove its first occurrence, and shift the remaining elements to fill the void created in Vector. Code: void removeRange(int lowerbound,int upperbound) It will remove all the elements within the given lower bound and upper bound indexes and shift the remaining values respectively. Example : Code: String s=v.remove(2); Reading the Elements in a Vector Use the following methods to get the elements stored in a Vector Code: Object firstElement() Use the first method to get the element stored at index 0. The second method is used to return the last object of the vector. The other method can be used to retrieve elements from any index in the Vector. Example : Code: String first=v.firstElement(); Check if a Vector is empty or not Following is the method to check if the Vector is empty or not. Code: boolean isEmpty() Example : Code: if(v.isEmpty()) Some more useful methods of Vector class Code: int indexOf(Object element) The above function returns the index of first occurrence of the element passed as parameter. Code: int indexOf(Object element,int index) The above function returns the index of first occurrence of the element passed as parameter beginning the search from the index passed as second parameter. Code: int lastIndexOf(Object element) The above function returns the index of last occurrence of the element passed as parameter. That means the search starts at the end point of the vector and goes backward. Code: int lastIndexOf(Object element,int index) The above function searches backwards for the specified object, starting from the specified index, and returns an index to it. Code: void setSize(int newSize) Code: String toString() Returns a string representation of this Vector, containing the String representation of each element. Code: void clear() Removes all of the elements from this Vector. A Sample Program Code: import java.util.*; That brings an end to this tutorial of using Vectors in Java. RE: [JAVA] A Guide to Using Vectors - Deque - 11-18-2012 The real value here lies in the example code, because the information about the methods you get by reading the API (which every programmer should do). Some additional information: Most of the time the ArrayList will be more suitable, unless you need synchronization. Expecially beginners won't deal with threads. The ArrayList is almost the same, just that it is not synchronized (therefor faster) and the ArrayList increases its size by 50%, whereas the Vectors default doubles its size when the underlying array is full. RE: [JAVA] A Guide to Using Vectors - Shining White - 11-18-2012 i remember i had a exam call SCJP / OCJP ![]() |