VECTORS
Hello guys, we are back with another blog today and in this
one we are going to talk about vectors. So what are vectors? Vectors are
resizable or dynamic array in java and they can be used to store objects and in
any number. Also if you have learnt
about Arraylist from the earlier blog, then you might be wondering what could
be the possible difference between a vector and an arraylist , so let’s learn about
vectors first and then we will see how it differs from an arraylist .
Let’s first see how to declare a Vector object from the vector class in java.
So first we’ll import the Vector class in java
import java.util.Vector;
And then we can make the declare the vector from the vector
class
Vector v = new Vector();
Here v is the name of our vector.
Now after declaring the vector we can add elements inside our
vector
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("To");
v.add("Coding");
v.add("Winds");
System.out.println(v);
//Output: [Welcome, To, Coding,
Winds]
}
}
So while declaring a Vector we can specify it’s capacity within the
parentheses after ‘Vector’, now if the number elements increases the specified
capacity, the Vector is upgraded to double it’s capacity(which was mentioned).
Also if we do not mention the size of the Vector, by default it will have a
size of 10 (which is also the default size of the ArrayList if we do not mention
it already).
Now let us see some of
the differences between Vectors and ArrayList in Java:
ArrayList |
Vector |
When we add
elements more than the capacity of the ArrayList then it get’s expanded by
half of it’s capacity |
When we add
elements more than the capacity of the Vector then it get’s expanded by
double it’s capacity |
It is not
synchronized and hence faster |
It is
synchronized and thus slower than Arraylist |
It was
introduced by JDK 1.2 |
It is a
legacy class |
It uses
iterator interface to traverse through it’s elements |
It can use
either of Iterator and enumerator to traverse through it’s elements. |
Now let’s discuss the various methods in Vectors :
1) add():
We have already seen in the above example that this method is
used to add elements inside Vector, now we can also use this method to add
elements at specific index
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add(0,"Welcome");
v.add(1,"Welcome");
v.add(2,"to");
v.add(3,"Coding");
System.out.println(v);
//Output: [Welcome, To, Coding,
Winds]
}
}
2) addAll():
This method is used to add a collection from an arraylist into a
vector:
Ex:
import java.util.Vector;
import java.util.ArrayList;
public class HackerRank {
public static void main(String[] args) {
ArrayList
<String> ar = new
ArrayList();
ar.add("Happy");
ar.add("Reading");
Vector v = new Vector();
v.addAll(ar);
System.out.println(v);
//Output: [Happy, Reading]
}
}
3) clear():
This method clears all the elements from the vector:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
v.clear();
System.out.println(v);
//Output: []
}
}
4) clone():
This method clones the vector into another:
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
Vector clonedVector = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
clonedVector = (Vector)v.clone();
System.out.println(clonedVector);
//Output: [Welcome, to, Coding,
Winds]
}
}
5
)capacity():
This method tell us the maximum capacity of the vector.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.capacity());
//Output:10
}
}
6) ensureCapacity():
This method resets the capacity of the vector:
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
v.ensureCapacity(22);
System.out.println(v.capacity());
//Output:22
}
}
7) get():
This method is used to get the element at a particular index:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.get(2));
//Output:Coding
}
}
8) indexOf():
This method returns the index of the first occurring element which
is specified inside the parentheses
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.indexOf("Winds"));
//Output:3
}
}
9) isEmpty():
This method returns true if the Vector is empty and
false if it is not.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.isEmpty());
//Output:false
}
}
10)
lastIndex():
This method returns the index of the last occurring element
specified in the list.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.indexOf("Winds"));
//Output:3
}
}
11)
remove():
This method returns true if the element which is specified inside
the parentheses is removed and if the specified element is not present inside
the Vector it returns false.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.remove("Hi"));
//Output:false
}
}
12)
insertElementAt():
This method inserts the specified elment at the specified index:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
Vector clonedVector = new
Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
v.insertElementAt("Readers",4);
System.out.println(v);
//Output:[Welcome, to, Coding,
Winds, Readers]
}
}
13)removeElement():
It does the opposite of the insertElement() method,
ie., removes the specified element.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
v.remove("Winds");
System.out.println(v);
//Output:[Welcome, to, Coding,
Winds]
}
}
14) lastElement() /firstElement() :
These methods returns the last and the first element of the vector
respectively.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.lastElement());
System.out.println(v.firstElement());
/*Output:Winds
Welcome */
}
}
15) setElementAt() :
This method modifies the element at the specified index in the
Vector, with the specified element (along with the index).
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
v.setElementAt("Winds people",3);
System.out.println(v);
//Output:[Welcome, to, Coding,
Winds people]
}
}
16) setSize():
Sets the size of the Vector to the integer specified inside the
parentheses.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
v.setSize(6);
System.out.println(v);
//Output:[Welcome, to, Coding,
Winds, null, null]
}
}
17) size() :
This method returns the size of the Vector:
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println(v.size());
//Output:4
}
}
18) equals():
This method returns true if the vector is equal to the
vector specified inside the parentheses, and false if it’s not equal.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector();
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
Vector v2 = new Vector();
v2.add("Hello");
v2.add("Readers");
Vector v3 = new Vector();
v3.add("Welcome");
v3.add("to");
v3.add("Coding");
v3.add("Winds");
System.out.println(v.equals(v2));
System.out.println(v.equals(v3));
/*Output:false
true */
}
}
19) trimToSize():
This method trims the capacity of the vector to it’s
size.
Ex:
import java.util.Vector;
public class HackerRank {
public static void main(String[] args) {
Vector v = new Vector(13);
v.add("Welcome");
v.add("to");
v.add("Coding");
v.add("Winds");
System.out.println("Initial Capacity: "+v.capacity());
v.trimToSize();
System.out.println("Capacity
after trimming: "+v.capacity());
/*
Initial Capacity: 13
Capacity after trimming: 4
*/
}
}
Note that, in Vectors there is difference in size and
capacity. Size (.size()) tells us the current size of the Vector that is the
number of elements it has, where as capacity (.capacity()) is the maximum
capacity of the Vector, after which it will be extended.
Hope you are clear on this topic do read our more articles on JAVA LANGUAGE.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
This article is SUBMITTED By : Pranjal Rai
Do subscribe to our daily blog update by clicking here.
Thank You!