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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vectors vs. Arrays in return

Status
Not open for further replies.

mlundin1

Technical User
Sep 15, 2000
7
US
This is a more general design question that anyone can comment on. I've created multiple methods that return vectors of like objects, and other methods that return arrays of like objects. Which is the best way to deal with return values?

I see the value of returning type-specific arrays, but when the number of elements aren't known, it's easier to create a Vector or an ArrayList object and return that. I do understand how easy it is to convert a Vector or ArrayList into an array, but is that the best thing to do?

I also see the issue of having to cast the returned Vector (or ArrayList) when dealing with the return value.

Any comments or experiences with this issue would be appreciated.

Thanks much,
Mike Lundin
 
Well, for one, Vectors are synchronized so you suffer a performance hit if you are using Vectors liberally through your code. You are better off passing around a List and then hiding the implementation of which kind of list internally. That way, anyone can use it seamlessly even if yoiu change the implementation (e.g. switch from LinkedList to ArrayList).

In general, Lists or other Collection objects are better 'containers' for objects than arrays. I tend to use arrays internally where it makes sense but whenever I expect that the container might need to grow, I use a List so I dont' have to deal with the API calls to bounds check and resize the array.

YMMV,

Charles
 
I was going to say something similar but I prefer to pass references to a Collection. This gives you the most flexibility in case you feel the need to change the implementation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top