JDK-6197797 : Inconsistent templates for toArray() in java.util
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_9
  • CPU: sparc
  • Submitted: 2004-11-19
  • Updated: 2010-04-02
  • Resolved: 2004-11-19
Related Reports
Duplicate :  
With the advent of generics, it seems entirely inconsistent to hava java.util.Vector, java.util.ArrayList, etc. have
public Object[] toArray().

rather, these should be public <T>[] toArray()

Since generics are now basically enforced (even though not explicitly required), the underlying data type in the elements array is known.

Further, it makes any code using this method particularly odeous, since you either have to recast element by element, or know the size of the array beforehand and then pass to toArray(<T>[]).

Once y'all decided to go to generics, you should go all the way.

Further, I would also like to express my deep displeasure with generics. All it did was mess up the code quite badly. Once of the NICEST features of Java was the fact that everything extended from Object, and thus Vector could hold anything. If the programmer couldn't keep the logic straight as to what was in a collection, then there are surely more serious problems in his/her code than just type matching. I know that one could rewrite Vector v = new Vector() with Vector<Object> v = new Vector<Objcet>(), but then again, this just looks horrid and is *very* difficult to read. With the introduction of generics, you fundamentally changed the style and format of Java. However, given the fact that since 1.1 your internal developers no longer have any standards, this isn't surprising.

So, enough of the rant ...

BUT, if you are going to go with generics, for godsake, make it consistent throught all classes that use them. There should no longer be ANY method that takes in Object or Object[] or returns Object or Object[]. Everything should be <T> or <T>[].