Home arrow static arrow Java Programming [Archive] - returning object Arrays vs Collections from a method
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - returning object Arrays vs Collections from a method
This topic has 18 replies on 2 pages.    « Previous | 1 | 2 |

Posts:2
Registered: 4/21/03
Re: returning object Arrays vs Collections from a method  
Aug 2, 2004 1:39 PM (reply 15 of 18)



 
Hi,

Continuing the argument...
Should a Vector be preferred or an array, if we are reading a resultset from a database query. Should the result be stored in an vector and then copy all the elements into an array??

Or is it more efficient to get the count of the result set(number of rows returned by the query) and store it an array, using the statements below.

Resultset rsRefSearch;
rsRefSearch.last();
int records = rsRefSearch.getRow();
rsRefSearch.beforeFirst();

Or just use Vectors instead of Arrays.

matetn
 

Posts:28
Registered: 7/28/04
Re: returning object Arrays vs Collections from a method  
Aug 2, 2004 1:56 PM (reply 16 of 18)



 
I would go with Galam_g's thoughts on this. The implementation suggested there covers best of both worlds scenario - a simulated paramerterized collection.
But coming back to the original question, although theoritically the points made by OP are valid - one can't just discount the use of Vectors without understanding the reason behind their usage. I think the reason for using Vectors will have something to do with them being dynamic and the array vs Vector tradeoff might be justified in that reason. I don't think any comments can be qualified without the knowledge of rationale.
 

Posts:2,909
Registered: 13.8.2003
Re: returning object Arrays vs Collections from a method  
Aug 2, 2004 10:51 PM (reply 17 of 18)



 
After teaching my 2 co-workers to use ArrayLists (and one of them still insists on rolling his own for small sets, you know, make an array of 20 and keep an index...don't let it grow) I grew to dislike people who prefer arrays (in the programmatical sense, otherwise he's cool).

Arrays are faster, arrays are typesafe, arrays arrays arrays...we have an object oriented language with a great collection API so why return arrays?

When you return an array, you're not thinking of further design. What if you need to perform collection methods on it? Sure you can always use Arrays.asList(), but that's gonna look really nice in the code...

In short, I'd like to thank you for wasting your time reading this and remember, when you use arrays instead of Collections, you're programming with Satan.

Thank You, Jussi
 

Posts:5,965
Registered: 5/17/03
Re: returning object Arrays vs Collections from a method  
Aug 2, 2004 11:16 PM (reply 18 of 18)



 
If Vectors are returned, client implementation ASSUMES
a specific object will be present in the collection.

Generics provide static type checking for collections.

An alternative is to return a wrapper class. It can contain an array, a collection or something else but users don't have to know. But when they request a member object they know what type it is.
 
This topic has 18 replies on 2 pages.    « Previous | 1 | 2 |