Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to create a Vector using Collections in java

how to create a Vector using Collections in java

Previous Home Next

 

In this example we are going to create a Vector.

For creating a Vector we have to use the java.util package which contains the java.util.Vector class. In this example we have created a class named CollectionExample inside which we have created a Vector in which we have added some elements.
 In the following example we have created a Vector object by using the default constructor of the Vector which is done as,
Vector<Integer> vect=new Vector<Integer>();
This statement
constructs an empty vector so that its internal data array
has size 10 and its standard capacity increment is zero. Here we have created a Integer vector.

The elements to this vector are added by using the add(int index) which have the following syntax,
boolean add(Object element)
This method appends the specified element to the end of this Vector.

We have also created the following methods,

void AddCollection(Vector vect, ArrayList al)
The above method add the elements of the ArrayList to the Vector. For this purpose we add some elements to the ArrayList and then added all the elements of the ArrayList to the Vector by using passing the ArrayList object al to the addAll(al) method as a parameter which is shown by the following statement in our example,
vect.addAll(al);

void Vectortoarray(Vector vect)
The above method will convert a Vector to a linear array. The above method takes the Vector object as an argument, also we have created an array of integer same size as that of the Vector. Then we have used the toArray(Anytypearray[] array) method on the object of the Vector i.e vect. Finally we have displayed all the values of the array by using the for loop.

void Createclone(Vector vect)
This method creates a clone of the given vector.This method takes the Vector object as an argument, we have also created another vector named vectclone which stores the values returned by using the clone() method on the object of the vector.The general syntax of the clone() method is,
Object clone()
This method returns a clone of the current vector.

void modify(Vector vect)
This method modifies the content of the given Vector which takes the object of the Vector as an argument. In this method we have used several insertion and removal method for manipulating the contents of a Vector.

In the following example we have created a Vector object by using the default constructor of the Vector which is done as,
Vector<Integer> vect=new Vector<Integer>();
This statement
constructs an empty vector so that its internal data array
has size 10 and its standard capacity increment is zero. Here we have created a Integer vector.

The elements to this vector are added by using the add(int index) which have the following syntax,
boolean add(Object element)
This method appends the specified element to the end of this Vector.

We have also created the following methods,

void AddCollection(Vector vect, ArrayList al)
The above method add the elements of the ArrayList to the Vector. For this purpose we add some elements to the ArrayList and then added all the elements of the ArrayList to the Vector by using passing the ArrayList object al to the addAll(al) method as a parameter which is shown by the following statement in our example,
vect.addAll(al);

void Vectortoarray(Vector vect)
The above method will convert a Vector to a linear array. The above method takes the Vector object as an argument, also we have created an array of integer same size as that of the Vector. Then we have used the toArray(Anytypearray[] array) method on the object of the Vector i.e vect. Finally we have displayed all the values of the array by using the for loop.

void Createclone(Vector vect)
This method creates a clone of the given vector.This method takes the Vector object as an argument, we have also created another vector named vectclone which stores the values returned by using the clone() method on the object of the vector.The general syntax of the clone() method is,
Object clone()
This method returns a clone of the current vector. The following statement of the example does that,
vectclone=(Vector<Integer>)vect.clone();

void modify(Vector vect)
This method modifies the content of the given Vector which takes the object of the Vector as an argument. In this method we have used several insertion and removal method for manipulating the contents of a Vector.

 
package r4r.co.in;
import java.util.*;


public class CollectionExample
{

/**
* @param args
*/

public static void main(String args[]) throws IllegalArgumentException, NullPointerException, ArrayIndexOutOfBoundsException
{
try
{
Vector<Integer> vect=new Vector<Integer>(); // creating a Vector having Integer elements
System.out.println("The initial capactiy of the vector is: "+vect.capacity()); // This method displays the initial capacity of Vector
vect.add(1); //adding elements to the vector
vect.add(2);
vect.add(3);
vect.add(4);
vect.add(5);
vect.add(6);
vect.add(7);

Iterator<Integer> itr=vect.iterator(); // Invoking Iterator on the vector
while(itr.hasNext())
{
System.out.println(itr.next()); // displaying the elements of the vector
}
ArrayList<Integer> al=new ArrayList<Integer>(); // creating an object of the arraylist
CollectionExample ce=new CollectionExample(); // creating an object of the CollectionExample
ce.Addcollection(vect, al); // calling the AddCollection method defined below
ce.Createclone(vect); // calling the Createclone method
ce.Modify(vect); // calling the modify method
ce.Vectortoarray(vect); // calling the Vectortoarray method
}

catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}

}

/* The following method converts a Vector to a simple array */
private void Vectortoarray(Vector<Integer> vect)
{
Integer[] array=new Integer[vect.size()]; // created a Integer array same size as that of vector
array=vect.toArray(array); // using toArray method on vecor vect
System.out.println("The contents of the created array are: ");
for(int i=0; i<array.length; i++)
{
System.out.println(array[i]); // displaying the elements of the array
}
Object[] array1=new Object[vect.size()]; // created an object array of same size as of vector
array1=vect.toArray(); // using toArray method on the vector
Integer[] arr=new Integer[vect.size()]; // defined an Integer array
for(int i=0; i<array1.length; i++)
{

arr[i]=(Integer)array1[i]; // placing the contents of the object array to Integer array via typecasting one by one

}
System.out.println("The contents of the object array are: ");
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i]); // displaying the contents of the created array
}

}

/* Following method creates a clone of the Vector created */
private void Createclone(Vector<Integer> vect)
{
Vector<Integer> vectclone=new Vector<Integer>(); // creating a vector name vectclone
vectclone=(Vector<Integer>)vect.clone(); // creating the clone of vect and storing in vectclone
System.out.println("The contents of the created vectorclone are: ");
Iterator<Integer> itr=vectclone.iterator(); // invoking iterator on vectclone
while(itr.hasNext())
{
System.out.println(itr.next()); // displaying the contents of the vectclone
}
}
/* Following method modifies a given Vector */

private void Modify(Vector<Integer> vect)
{
vect.add(9); // adding element to the current vector
vect.add(8);
vect.insertElementAt(70, 7); // inserting 70 at the index 7
vect.setElementAt(17, 2); // replacing 17 as value on index value 2
vect.remove(6); // removes the value 6 in the vector
vect.remove(1); // removes the value from index 1 in the vector
System.out.println("After modifying the vector, now the vector contains the elements: ");
Iterator<Integer> itr=vect.iterator(); // Invoking iterator on the vector
while(itr.hasNext())
{
System.out.println(itr.next()); // displaying the values of the vector
}
}

/* Following method adds some collection to the current Vector */

private void Addcollection(Vector<Integer> vect, ArrayList<Integer> al)
{
al.add(14); // adding elements to the arraylist
al.add(15);
al.add(16);
al.add(17);
al.add(18);
vect.addAll(al); // adding the arraylist to the vector using the addAll() method
System.out.println("After adding the collection Arraylist to Vector, Vector contents are: ");
Iterator<Integer> itr=vect.iterator(); // Invoking iterator on the vector
while(itr.hasNext())
{
System.out.println(itr.next()); // displaying the contents of the vector
}
}


}




The output of the above given example is as follows:

The initial capactiy of the vector is: 10
1
2
3
4
5
6
7
After adding the collection Arraylist to Vector, Vector contents are:
1
2
3
4
5
6
7
14
15
16
17
18
The contents of the created vectorclone are:
1
2
3
4
5
6
7
14
15
16
17
18
After modifying the vector, now the vector contains the elements:
1
17
4
5
6
70
14
15
16
17
18
9
8
The contents of the created array are:
1
17
4
5
6
70
14
15
16
17
18
9
8
The contents of the object array are:
1
17
4
5
6
70
14
15
16
17
18
9
8

Previous Home Next