It returns nothing.įor example: v.add(4, “John”) // It adds the element ‘John’ at position 4 in the vector.ģ. void add(int index, Object o): This method is used to insert a specified element at a specific position into the vector list. It will return true if the specified element is added successfully into vector list otherwise, it will return false.Ģ. boolean add(Object o): It is used to add the specified element to the end of given vector list. Vector class provides many different methods to create, access, and modify the data structure. Vector v = new Vector(3,6) Vector Methods in Java Vector vec = new Vector() // It will store only string type element. We can also create a vector object in generic form like this: Vector vec = new Vector() // T is the type of generic. Vector v = new Vector(list1) // list1 is elements of the collection.Ĭode to create Generic Vector class object Vector v = new Vector(int initialCapacity, capacityIncrement) Vector vec = new Vector(3) // It will create an empty vector with initial capacity of 3.ģ. Vector vec = new Vector(int initialCapacity) Similarly, if we remove an element at a specific position, the same process is performed by vector and elements will be shifted one position up.īut vector is not a good choice in inserting and removal of elements because it will take more time to copy elements into new array and will get slower as the size of the vector grows. Look at the below figure to understand better. When we add an element at a specific position, the vector uses a System.arraycopy() method to move the element from that specific position to the one position down into the array. So, its size will be double by default that will be 20.Īfter resizing, it copies all original elements of the old array into the new array using System.arraycopy() method, and then it will insert 11th element. In this object creation, we did not specify any capacity increment in the constructor. Therefore, it is important to assign the value of initial capacity and capacity increment for minimizing the number of resizing operations without wasting too much memory and time. A new internal array will create which copies all the original elements of old array into the new array by using System.arraycopy() method.Ĭreating a new array and copying elements will take more time. If you do not specify an initial capacity and capacity increment, each time vector will be resized due to exceeding its capacity. a new internal array will be automatically created that is equal to the size of old array plus capacity increment specified in the constructor. When the 11th element is inserted into vector, the capacity of vector will be exceeded. Inside the vector list, data is stored as an array of objects according to the initial size that you specify in the constructor. It creates a vector list with a default initial capacity of 10. There are four ways to create an object of vector class in Java. Ways to create Vector class object in Java It creates a vector that contains the element of collection c. It creates an empty vector with specified initial capacity and capacity increment. Vector(int initialCapacity, int capacityIncrement) It creates an empty vector with specified initial capacity. It creates an empty vector with default initial capacity of 10. Vector class provides four types of constructors to create, access, and modify the data structure. Vector is the best choice if the frequent operation is retrieval (getting). It can be iterated by a simple for loop, Iterator, ListIterator, and Enumeration.ġ0. Vector is rarely used in a non-multithreaded environment due to synchronized which gives you poor performance in searching, adding, delete, and update of its element.ĩ. Vector class is preferred where we are developing a multi-threaded application but it gives poor performance because it is thread-safety.Ĩ. Only one thread can access can enter to access vector object at a time.ħ. Two threads cannot access the same vector object at the same time. Most of the methods present in the vector class are synchronized. Therefore, it can hold elements of any type and any number.Ħ. Heterogeneous elements are allowed in the vector class. Null elements are allowed in the Java vector class.ĥ. It preserves the insertion order in Java.Ĥ. Duplicate elements are allowed in the vector class.ģ. The underlying Data structure for vector class is the resizable array or growable array.Ģ. The current hierarchy diagram of a Vector class is shown in the below figure.ġ. It also implements three marker interface such as serializable, cloneable, and random access interface. Vector class implements List interface and extends AbstractList. Hierarchy Diagram of Vector class in Java
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |