Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 12 June 2017

Vector Java

 Java Vector Class

Vector is a class in java collection which is available in java.util.* package. Vector class implements List interface and extends AbstractList class.

Vector is a legacy(i.e old class) class in java collection framework.

Java Vector class implements a growable array of objects. Like an array, it contains the component that can be accessed using an integer index. However, Vector's size can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.


Some Points About Java Vector Class

  • Vector class implements List interface and extends AbstractList class.
  • Vector class can contain duplicate elements like ArrayList and LinkedList class.
  • Vector class maintains insertion order of an elements.
  • Vector is synchronized.
  • Vector is a legacy class in collection.
  • Default capacity of Vector is 10.
  • Vector uses Enumeration interface to traverse the elements but you can use Iterator interface to traverse the elements with vector class.

Hierarchy of Vector Class

Vector Java
In the above picture, you can see Vector class extends AbstractList class and AbstractList class implements List interface.


Constructors of Vector Class

There is some constructor of Vector class in java.

1) Vector()

This constructor creates a default vector which has an initial size of 10 and its standard capacity increment is 0(zero).

2) Vector(Collection c)

This constructor constructs a vector that contains the elements of collection c.

3) Vector(int initialCapacity)

It constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.

4) Vector(int initialCapacity, int capacityIncrement)

It constructs an empty vector with the specified initial capacity and with its capacity increment.


Methods of Vector Class

We will see here some extra methods of vector class.

1) boolean add(Object o)

This method appends the specified elements to the end of the vector.

2) boolean addAll(Collection c)

This method is used to appends all of the elements in the specified Collection to the end of this vector.

3) void add(int index, Object element)

It is used to inserts the specified element at the specified position in the vector.

4) void addElement(Object o)

This method adds the specified component to the end of this vector, increasing its size by one.

5) int capacity()

It returns the current capacity of the vector.

6) void clear()

It removes all of the elements from the vector.

7) Object clone()

It returns a clone of the vector.

8) boolean contains(Object o)

It returns true if this vector contains the specified elements.

9) boolean containsAll(Collection c)

Returns true if this vector contains all of the elements in the specified Collection c.

10) void copyInto(Object[] anArray)

Copies the components of this vector into the specified array.

11) void trimToSize()

This method trims the capacity of the vector to be the vector's current size.

12) String toString()

This method returns a String representation of this vector and containing the String representation of each element.

13) int size()

This method returns the number of components in this vector.

14) void setSize(int newSize)

Set the size of the vector.

15) boolean isEmpty()

This method tests it this vector has no components.


Java Vector Example

This is a simple example of vector class where we will use Enumeration interface to traverse the elements and use personal methods of vector class.

import java.util.*;
class VectorExample
{
public static void main(String args[])
{
//creating Vector 
Vector<String> v = new Vector<String>();
v.add("america");//collection's method
v.addElement("india");// vector's method
v.addElement("china");
v.addElement("england");

//using Enumeration for traversing elements
Enumeration e = v.elements();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
}


output : america
              india
              china
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate