Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Vector problem

Thread view: 
anshul - 07 Dec 2006 22:55 GMT
Hi I wish to add object in a list of vectors
But when I add the same object to the vector list, it is overwritten to
that list, instead of being appended.
Here is my code :

String r;
ResultWrapper rw = new ResultWrapper();
ResultWrapper rw1 = new ResultWrapper();
Vector <ResultWrapper> vec = new Vector <ResultWrapper>();
rw.setJobName("Job1");
rw.setJobId("IDJob1");
vec.addElement(rw);
rw.setJobId("IDJob2");
rw.setJobName("Job2");
vec.addElement(rw);

ListIterator iter = vec.listIterator();
while(iter.hasNext())
{
rw1 = (ResultWrapper) iter.next();
r = rw1.getJobName();
System.out.println("Job name is " + r);
r = rw1.getJobId();
System.out.println("Job Id is " + r);

} // end of while iter.next()

Here is the output of my code :

Job name is Job2
Job Id is IDJob2
Job name is Job2
Job Id is IDJob2

How to get this output?
Job name is Job1
Job Id is IDJob1
Job name is Job2
Job Id is IDJob2
Chrisie - 07 Dec 2006 23:07 GMT
Your problem is in the lines:
rw.setJobId("IDJob2");
rw.setJobName("Job2");
What you are doing here is changing the values of the JobName and JobId
stored in the object to which you are using rw as a reference. This
results in changing the values of the object you have already in the
Vector, and then with the line
vec.addElement(rw);
you add it in the vector again and this is why when you are printing
the vector you get the same thing twice.
What you should do to avoid this is create a new ResultWrapper object
after adding the first one in the vector and then give it the values
you want like this:
rw.setJobName("Job1");
rw.setJobId("IDJob1");
vec.addElement(rw);
rw=new ResultWrapper();
rw.setJobId("IDJob2");
rw.setJobName("Job2");
vec.addElement(rw);
anshul - 07 Dec 2006 23:10 GMT
Thanks man!
> Your problem is in the lines:
> rw.setJobId("IDJob2");
[quoted text clipped - 16 lines]
> rw.setJobName("Job2");
> vec.addElement(rw);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.