Add, Remove & Display student name from Vector
Vector implements a dynamic array. It is similar to ArrayList, but with two differences −
Program :-
Output:-
Vector implements a dynamic array. It is similar to ArrayList, but with two differences −
·
Vector is
synchronized.
·
Vector contains many
legacy methods that are not part of the collections framework.
Vector
proves to be very useful if you don't know the size of the array in advance or
you just need one that can change sizes over the lifetime of a program.
Following
is the list of constructors provided by the vector class.
Vector( )
n creates a default vector, which has an initial size of 10.
Vector(int size)
n creates a vector whose initial capacity is specified by size
Vector(int size, int
incr)
n creates a vector whose
initial capacity is specified by size and whose increment is specified by
incr. The increment specifies the number of elements to allocate each time
that a vector is resized upward
Vector(Collection c)
creates a vector that contains
the elements of collection c.
Vector defines three protected data
member:
§ int capacityIncreament: Contains the increment value.
§ int elementCount: Number of elements currently in vector stored in it.
§ Object elementData[]: Array that holds the vector is stored in it.
Methods of Vector class
Adds the specified
component to the end of this vector, increasing its size by one.
Returns the current
capacity of this vector.
Returns a clone of this
vector.
Tests if the specified
object is a component in this vector.
Copies the components of
this vector into the specified array.
Returns the component at
the specified index.
Returns an enumeration of
the components of this vector.
Increases the capacity of
this vector, if necessary, to ensure that it can hold at least the number of
components specified by the minimum capacity argument.
Returns the first
component of this vector.
Searches for the first
occurence of the given argument, testing for equality using the
equals
method.
Searches for the first
occurence of the given argument, beginning the search at
index
,
and testing for equality using the equals
method.
Inserts the specified
object as a component in this vector at the specified
index
.
Tests if this vector has
no components.
Returns the last
component of the vector.
Returns the index of the last
occurrence of the specified object in this vector.
Searches backwards for
the specified object, starting from the specified index, and returns an index
to it.
Removes all components
from this vector and sets its size to zero.
Removes the first
occurrence of the argument from this vector.
Deletes the component at
the specified index.
Sets the component at the
specified
index
of this vector to be the specified object.
Sets the size of this
vector.
Returns the number of
components in this vector.
Returns a string
representation of this vector.
Trims the capacity of
this vector to be the vector's current size.
Algorithm:
- start
- create
an object of Vector class
- add
five student names to Vector using addElement() method of Vector class
- select
option from menu
- if
option is , then add new student name to Vector using addElement() method
of Vector class
- if
option is 2, then remove specific element from Vector using
removeElement() method of Vector class
- if
option is 3, then display all Vector elements using Enumeration class
methods nextElement() and hasMoreElements().
- stop
import
java.util.Vector;
import
java.util.*;
class
c
{
public
static void main(String[] args) throws Exception
{
Scanner
s = new Scanner(System.in);
Vector v=new Vector
();
int i,j=0,n;
do
{
System.out.println("enter
choice"+"\n1. Add"+"\n2. Remove"+"\n3.
Display" +"\n4. Exit");
n=s.nextInt();
switch(n)
{
case
1:System.out.println("enter name of student");
v.add(j,s.next());
System.out.println("==================\n");
j++;
for(i=0;i<v.size();i++)
{
System.out.println((i+1)+"
"+v.get(i));
}
System.out.println("");
break;
case
2:System.out.println("enter the no of Student ");
v.remove((s.nextInt()-1));
System.out.println("==================\n");
for(i=0;i<v.size();i++)
{
System.out.println((i+1)+"
"+v.get(i));
}
System.out.println("");
break;
case
3:System.out.println("==================\n");
for(i=0;i<v.size();i++)
{
System.out.println((i+1)+"
"+v.get(i));
}
System.out.println("");
break;
}
}while(n!=4);
}
}
enter choice
1. Add
2. Remove
3. Display
4. Exit
1
enter name of student
Chetan
==================
1 Chetan
enter choice
1. Add
2. Remove
3. Display
4. Exit
1
enter name of student
Mayank
==================
1 Chetan
2 Mayank
enter choice
1. Add
2. Remove
3. Display
4. Exit
2
enter the no of Student
2
==================
1 Chetan
enter choice
1. Add
2. Remove
3. Display
4. Exit
==================
Observations :-
Here we observed
that how child class inherit methods and
properties of parent class.
Conclusion:
We
implemented different types of Inheritance.
Simply Important..!!
ReplyDeleteThanks
Delete