I have two classes EmpData and VectorHandler.
EmpData contains info abt employees like name and empcode
Vectorhandler has a vector which stores Empdata objects .
I want to use removeElement() which will delete the object which contains the particular empname/empcode from the vector.
This doesnot work:

v.removeElement(empcode);

as empcode is not of type object but a simple int variable.
How do i delete the particular object which contains that particular empcode

Well you'd have to search through the Vector; once you've found the code, you'd have access to the Object you need to remove and thus v.remove(empData); will work. The easiest way to iterate over the Vector is:

for (EmpData empData: v)
    if (empData.code == empCode)
        v.removeElement(empData);

Here I assume that you declared v as: Vector<EmpData> v = new Vector<EmpData>(); . Since Vector objects are iterable, I was able to use the for loop as seen above where empData refers to a single element in the Vector v. Hope that helps! I also hope you recognize that removeElement() is a method designed to remove an existing Object within the Vector that matches the input of removeElement().

But code is a private variable in my EmpData class.
Heres my code:

//PRogram to demonstrate interaction between a class object and a vector

import java.lang.*;
import java.io.*;
import java.util.*;

 class EmpData { 
	private String name = new String();
	private int code = 0;
	int c = 0;
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	
	
	EmpData(){
	 try{
	 	System.out.println("Enter empcode : ");
	     code = Integer.parseInt(in.readLine());
	 	}catch(Exception e){}	
	  
	  
	}
	
	
	void display(){
		System.out.println("You enterd : " + code);
	}	
}

class VectHandler { 
	
	   private Vector<EmpData> vect = new Vector<EmpData>();
	   void add_vect(){
	     EmpData d = new EmpData();
	   	 vect.addElement(d);
	   	 System.out.println("obj added!");
	   }
	   
	   void disp_vect(){
	   	for(int i = 0; i<vect.size(); i++){
	     ((EmpData)vect.elementAt(i)).display();
	   }
	   }
	   void delete_vect(){
	   	System.out.println("Enter the code to be deleted:");
	   	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	   	int empcode = Integer.parseInt(in.readLine());
	   	for (EmpData empData: vect){
	   	    if (empData.code == empcode)        
	   	    vect.removeElement(empData);
          }
	   }
	}
	

class test2_ED_vect {
 public static void main(String args[])	{
    
 	VectHandler v = new VectHandler(); //used to handle implementing vectors from VectorHandler class   

//this code is used for implementing vectors from main   
/* 	Vector v = new Vector();
 	v.addElement(new ED("Hello"));
 	v.addElement(new ED("Mello"));
    ((ED)v.elementAt(0)).display(); 	
    ((ED)v.elementAt(1)).display(); 	
*/ //this code works fine with no VectorHandler defined/declared 


 	v.add_vect();
 	v.add_vect();
 	v.add_vect();
 	v.disp_vect();
 	v.delete_vect();
 	v.disp_vect();
 	
 }
}

compiler gives me this error:
--------------------Configuration: JDK version 1.5.0_06 <Default>--------------------
C:\Program Files\JCreator LE\MyProjects\string1\test2_ED_vect.java:48: code has private access in EmpData
if (empData.code == empcode)
^
1 error

Process completed.

So add a method to the object that will allow you to compare it with another value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.