• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Should an ArrayList be used in a Bean?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I am writting my Beans I wonder whether you shouldn't use an ArrayList (or other collections) as properties of the Bean. The reason why is because I believe that the actual object contents of the ArrayList are hidden anyone else using the Bean. I would think it more appropriate to convert to a normal array before adding to your Bean. Doing this allows others to see what data type the property is. e.g.

class ExampleBean{
private ArrayList cars; //what is contained in this ArrayList?
private Ford[] cars; //it is clear what this is now.

//getters and setters here...

}

So, is it ok to use collections or is it best practice to convert to an array so that the datatype is clear to other developers?

Thanks heaps.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why shouldn't you use Collections in your classes?
[ July 14, 2004: Message edited by: Max Habibi ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concern in the original post seems to be that the data type(s) of the components in the collection are lost at compile time. (Note that this can be easily overcome with the use of generics in Java 5.) So, the question appears to concern whether it is preferable to make use of a more strongly typed array.

I'm moving this thread to the Intermediate forum. But before I do...

red herring,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Obviously fictitious display names don't comply.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Felix Clarke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my case I have several transfer objects and a facade. This facade uses and creates several transfer objects. These transfer objects may sometimes contain other transfer objects within one property that I have stored within an ArrayList. The problem is how is anyone intuitively to know (without referring to doco) what this ArrayList actually contains. Whereas, if this where not a collection object but a simple array it is clear what it is.

So my question is not about whether is will compile but what is best practice?
 
Felix Clarke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put it this way, forget the property example and look at this signature:



This ArraysList contains several EmailTO transfer objects - but I had to tell you this. Now look at this signature:



See how it is self explaining. Is there some "best practice" out there regarding this?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, your method shouldn't use ArrayList as a return type at all, but List or Collection.

Second, as already pointed out, this will get much better with Tiger. Until then, our team has decided to already use Tiger's syntax for generics, but put it into comments (as the compiler wouldn't understand it yet). That is, the method would look like

 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by red herring:
As I am writting my Beans I wonder whether you shouldn't use an ArrayList (or other collections) as properties of the Bean. The reason why is because I believe that the actual object contents of the ArrayList are hidden anyone else using the Bean. I would think it more appropriate to convert to a normal array before adding to your Bean. Doing this allows others to see what data type the property is. e.g.

class ExampleBean{
private ArrayList cars; //what is contained in this ArrayList?
private Ford[] cars; //it is clear what this is now.

//getters and setters here...

}

So, is it ok to use collections or is it best practice to convert to an array so that the datatype is clear to other developers?

Thanks heaps.



HI Felix,

I can see where you're coming from. Arrays are more normative, and for a lot of reasons(distributed computing being one), they are the better choice. But. ArrayLists are also useful for these sorts of situations, and not really a terrible answer.

The bottom line, IMO, is this. If you're the person who sets the standards for your team, and your efficiency doesn't suffer too much, do what you think is right

All best,
M
 
reply
    Bookmark Topic Watch Topic
  • New Topic