Collections

Vector methods Java Code With Examples

Table of Contents

Program

import java.util.Vector;

public class VectorWithMethods 
{
	public static void main(String[] args) 
	{
			Vector<String> ve = new Vector<String>();
			
                        //add(int index,E element)
                        ve.add(0, "Java");
			ve.add(1, "Is a");
			ve.add(2, "Indipendent");
			ve.add(3, "Programming");
			ve.add(4, "Language");
			System.out.println("1.Vector boolean add(int index,E element) :-" 
					+ "\n" + ve);
			System.out.println("");
			
                        //add(E e)
                        System.out.println("2.Vector void add(E e) :-");
		for(String xe : ve)
		{
			System.out.println(xe);
		}
			System.out.println("");
                        // size()
			System.out.println("3 .Vector Int Capacity :-" + "\n" + ve.capacity());
			System.out.println("");
			
                        //toString()
                        System.out.println("4 .Vector toString IS :-" + "\n" + ve.toString());
			System.out.println("");
			

                        //remove​(int index)
                        System.out.println("5 .Vector Element Removed :-"   + ve.remove(1));
			System.out.println("After Remove :-"   + ve);
	}

}

Output

 1.Vector boolean add(int index,E element) :-
[Java, Is a, Indipendent, Programming, Language]

2.Vector void add(E e) :-
Java
Is a
Indipendent
Programming
Language

3 .Vector Int Capacity :-
10

4 .Vector toString IS :-
[Java, Is a, Indipendent, Programming, Language]

5 .Vector Element Removed :-Is a
After Remove :-[Java, Indipendent, Programming, Language]

Description

public E remove​(int index)

Removes the element at the specified position in this Vector. Shifts any subsequent elements
to the left (subtracts one from their indices). Returns the element that was removed from the Vector.

Specified by:

remove in interface List

Overrides:

remove in class AbstractLis

Parameters:

index – the index of the element to be removed

Returns:

element that was removed

Throws:

ArrayIndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size() )

public void add​(int index, E element)

Inserts the specified element at the specified position in this Vector. Shifts the element currently
at that position (if any) and any subsequent elements to the right (adds one to their indices).

Specified by:

add in interface List<E>

Overrides:

add in class AbstractList<E>

Parameters:

index – index at which the specified element is to be inserted
element – element to be inserted

Throws:

ArrayIndexOutOfBoundsException – if the index is out of range (index < 0 || index > size() )

public int size()

Returns the number of components in this vector.

Specified by:

size in interface Collection<E>

Specified by:

size in interface List<E>

Returns:

the number of components in this vector

public String toString()

Returns a string representation of this Vector, containing the String representation of each element.

Overrides:

toString in class AbstractCollection<E>

Returns:

a string representation of this collection

public E remove​(int index)

Removes the element at the specified position in this Vector. Shifts any subsequent elements to the
left (subtracts one from their indices). Returns the element that was removed from the Vector.

Specified by:

remove in interface List<E>

Overrides:

remove in class AbstractList<E>

Parameters:

index – the index of the element to be removed

Returns:

element that was removed

Throws:

ArrayIndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size())