Hello friends ,
I have a problm accessing my objects in vectors
what i am trying to do is store objects ( of a class defined by me) in a vector.
I have two classes VectorHandler and Emp_database.
In VectorHandler, I have a vector (Vector vect) which stores objects of Emp_database passed from the main() func.
In Emp_database , I have a string (name) which stores name of an employee.
I can pass an object from main() like:

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

class ED{ //class object
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	String name = new String();
	void add(){
	     System.out.println("Enter name  : ");		
		 name = in.readLine();	
		catch(IOException e){System.out.println("Error!");}
	void display(){
		System.out.println("You enterd : " + name);

class VectHandler extends ED{ //extends ED
	   Vector vect;
	   void add_vect(ED e){
	   	 add(); //will first store the data
	   	 vect = new Vector();
	   	 System.out.println("obj added!");
	   void disp_vect(){
	   	 //ED e = new ED();
	   	 //e = (e)vect.elementAt(0);
	   	 System.out.println(vect); //displays [ED@923e30]
class test2_ED_vect{
 public static void main(String args[])	{
 	ED  rec = new ED();
 	VectHandler v = new VectHandler();
 	v.disp_vect(); //displays [ED@923e30]


What i want to do is display the name of the employee in the vector class

Recommended Answers

All 10 Replies

Look at this (from an earlier attempt of yours which is almost right):

//ED e = new ED();
//e = (e)vect.elementAt(0);

Is the item stored in the vector of type "e", or of type "ED", because the type is what you need to cast to, not the variable.

You should, of course, also place this in a loop using the loop counter rather than 0. Then again, you could use the new for loop and not worry about casting or the counter.

Thanks for the reply..
I modified the code and used type instead of variable
and the program runs fine..
Thank you so much!

But i have a new problem regarding the sae program
What I am trying to do is pass a value from the VectorHandler to EmpData by creating an object of EmpData in the VectoraHandler.
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();
	EmpData(String n){
	  name = n;
	void display(){
		System.out.println("You enterd : " + name);

class VectHandler extends EmpData{ 
	   private Vector vect = new Vector();
	   String n = new String("abhi");
	   EmpData d = new EmpData(n);
	   void add_vect(){
	   	 System.out.println("obj added!");
	   void disp_vect(){

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"));
*/ //this code works fine with no VectorHandler defined/declared 


But the compiler displays this error:
--------------------Configuration: JDK version 1.5.0_06 <Default>--------------------
C:\Program Files\JCreator LE\MyProjects\string1\test2_ED_vect.java:19: cannot find symbol
symbol : constructor EmpData()
location: class EmpData
class VectHandler extends EmpData{
Note: C:\Program Files\JCreator LE\MyProjects\string1\test2_ED_vect.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Process completed.

Simply add

EmpData() {}

to your EmpData class for the first error.

For the second error, you are not using Generics and are using JDK 1.5 or later. It's a warning (a note) and it won't stop you compiling the program, but you really should read up on Generics (check out the "New Features" portion of the API docs for JDK 1.5) and use them.

Hey it works..
Gosh I remember I had faced a similar issue for one of my programs in c++ and I dont know how but when I added that code..it worked!..
Cud you explain why this happens even though you specify the constructor to be called precisely?

Already done in Sun forums, but I'll do it again here.

The compiler will automatically create a default constructor for your class, but only if you don't declare any constructors at all.

Whenever a constructor is called, if that contructor does not contain a call to a specific super class class constructor, then the compiler will automatically add a call to the default constructor of the super class.

Since you created a constructor in EmpData, it has no default constructor.

Since VectorHandler has no constructor, the compiler creates a default constructor, which calls the default constructor of the super class (EmpData). That constructor does not exist, however.

Yes. got that.

But I have a new problem..
I want to use removeElement() which will delete the object which contains the empname/empcode from the vector.
This doesnot work:


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

Loop through the vector comparing the "empcodes" contained in the contained objects then delete the matching one.

Thank you again. It works:)

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.