Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing stuff in Vectors

Status
Not open for further replies.

Elleri

Programmer
May 23, 2001
2
US
Greetings all, I have another question. I'm working on building a vector and everytime I add another item, it replaces all the items in the vector with that item, so all prior additions are replaced. I'm using this:
customerList.addElement(cust2);
where customerList is my vector, and cust2 is an object from one of my classes. It is contained in a while loop that keeps checking if the user wants to add more customers. addElement appends to the end of the vector right? If so, what would I be doing that would make it overwrite all the other entries in the vector? The vector is growing just fine, just everything gets replaced with the last entry. It's probably something really dumb but I just don't see it. Thanks in advance for any help/suggestions.
 
Your adding everything with the same reference - cust2. Therefore all previous references to cust2 now point to the most recent addition to the vector. Use a counter to incrementally add to the vector ie.

vector.addElement( ("Cust" + counter) )

to give each element in the vector a unique name.

By the way, if you do note need all the synchronisation overhead of Vectors, ArrayLists perform faster as they aren't synchronised.
 
Hi qsiSimon,

I don't think this is the case. I think it have something to do with the codes instead. Some loops errors? Perhaps Ellerie would like to paste out some of your codes?

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top