Vectors in Java.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Vectors In Java Vectors  Vector implements a dynamic array which means it can grow or shrink as required.  Unlike array, we can store n-number of elements in it as there is no size limit.  It is a part of Java Collection framework since Java 1.2. It is found in the java.util package  L...

Vectors In Java Vectors  Vector implements a dynamic array which means it can grow or shrink as required.  Unlike array, we can store n-number of elements in it as there is no size limit.  It is a part of Java Collection framework since Java 1.2. It is found in the java.util package  Like an array, it contains components that can be accessed using an integer index. Constructors of Vector Class 1. Vector(): Creates a default vector of the initial capacity is 10. Vector v = new Vector(); 2. Vector(int size): Creates a vector whose initial capacity is specified by size. Vector v = new Vector(int size); 3. Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr. It specifies the number of elements to allocate each time a vector is resized upward. Vector v = new Vector(int size, int incr); 4. Vector(Collection c): Creates a vector that contains the elements of collection c. Vector v = new Vector(Collection c); Vector creation Examples 1. Integer Vector Vector v = new Vector(5); 2. String Vector: Vector vec = new Vector(); Various operations on Vector class 1. Adding elements 2. Updating elements 3. Removing elements 4. Iterating over elements Operation 1: Adding Elements  In order to add the elements to the Vector, we use the add() method.  This method is overloaded to perform multiple operations based on different parameters.  They are listed below as follows: 1.add(Object): This method is used to add an element at the end of the Vector. Vector v1=new Vector(5); V1.add(10); 2.add(int index, Object): This method is used to add an element at a specific index in the Vector. Vector v1=new Vector(5); V1.add(0,10); Operation 2: Updating Elements  After adding the elements, if we wish to change the element, it can be done using the set() method.  Since a Vector is indexed, the element which we wish to change is referenced by the index of the element.  Therefore, this method takes an index and the updated element to be inserted at that index.  Example: Vector.set(index,element); Vector v2=new vector(); V2.set(2,5); Operation 3: Removing Elements In order to remove an element from a Vector, we can use the remove().  remove(Object): This method is used to remove an object from the Vector. If there are multiple such objects, then the first occurrence of the object is removed.  remove(int index): Since a Vector is indexed, this method takes an integer value which simply removes the element present at that specific index in the Vector. After removing the element, all the elements are moved to the left to fill the space and the indices of the objects are updated. Operation 4: Iterating through Vector  There are multiple ways to iterate through the Vector.  The most famous ways are by using the basic for loop in combination with a get() method to get the element at a specific index and the advanced for a loop. Some other Important Methods  size(): This method returns the number of components in this vector  capacity(): This method returns the actual capacity of the vector  boolean isEmpty(): Checks if vector is empty  clear(): Removes all elements from the vector  indexOf(element): Returns the location of element provided in argument  trimToSize() Trims vectors capacity to its current size

Use Quizgecko on...
Browser
Browser