Object arrays are inferior to collections in almost every way. Prefer Set
,
List
, or Multiset
over an object array whenever possible.
A few of these issues are covered, in much greater detail, in Effective Java Item 28: Prefer lists to arrays.
Don’t use object arrays as method parameters:
public void createUsers(User[] users) { ... }
Use an Iterable
instead:
public void createUsers(Iterable<User> users) { ... }
Don’t use object arrays as method return values:
public User[] loadUsers() { ... }
Use an ImmutableList
(or ImmutableSet
) instead:
public ImmutableList<User> loadUsers() { ... }
If you have a 2-dimensional array (e.g., Foo[][]
), consider using an
ImmutableTable<Integer, Integer, Foo>
instead.
Suppress false positives by adding the suppression annotation @SuppressWarnings("AvoidObjectArrays")
to the enclosing element.