Use collection classes, unless you have a specific reason to use arrays. So, I'd choose option 2.
There are a number of other comments that can be made about your code.
First, It's better to program to an interface, not an implementation - make the member variable BList
in class A
a List<B>
instead of an ArrayList<B>
. Also, member variables should be private
(unless there's a very good reason for them not to be private
). Third, use generics whenever possible - why does your method getBList()
return a raw ArrayList
? The de-facto naming standard for variables in Java is camel case, starting with a lower-case letter. So don't call the member variable BList
, but bList
(or some other, better name).
class A {
private List<B> bList = new ArrayList<B>();
public List<B> getBList() {
return bList;
}
}
Is it really necessary for class Facade
to return a B[]
?
Another point to consider is to make your class A
immutable, because unexpected things might happen if you'd get the list from an A
object, and then add or remove elements from the list (the list inside your A
object will also be changed and that can be confusing). You'd have to return a read-only view of the list in your getBList()
method:
public List<B> getBList() {
return Collections.unmodifiableList(bList);
}