Remove a value with remove() method

Started by VelMurugan, Nov 25, 2008, 04:39 PM

Previous topic - Next topic

VelMurugan

Remove a value with  remove() method

In this section you will learn to remove an object from a particular position specified by a  index value. Here is an example that provides the usage of the remove() method in more detail.

Create a class "VecRemove" with an vector. Populate it with the integer objects using add(int,object) method. Now remove an object from a particular position specified by a index value using the remove() method. Specify the index position from where you want to remove an object.

Here is the Code of the Example :

VecRemove.java :
import java.util.*;
public class VecRemove{
    public static void main(String argv[]){
    Vector v = new Vector();
    v.add(0,new Integer(10));
    v.add(1,new Integer(20));
    v.add(2,new Integer(30));
    v.add(3,new Integer(40));
    v.add(4,new Integer(50));
    v.add(5,new Integer(60));
    System.out.println("The actual Vector Size: " + v.size());
    for(int i=0; i < v.size();i ++){
        Integer iw =(Integer) v.get(i);
        System.out.println(iw);
    }
     v.remove(3);
    System.out.println("After removing the element from
            the position 3, Vector Size: " + v.size());
    for(int i=0; i < v.size();i ++){
        Integer iw =(Integer) v.get(i);
        System.out.println(iw);
    }
  }
}


The Output Of The Example is as shown:

C:\roseindia>javac VecRemove.java

C:\roseindia>java VecRemove
The actual Vector Size: 6
10
20
30
40
50
60
After removing the element from the position 3, Vector Size: 5
10
20
30
50
60


Source : RoseIndia