When is an ArrayList not an ArrayList?


Hey guess what yall? Did you know that java.util.Arrays$ArrayList is not an ArrayList? I do! I really shouldn’t be endowed with such information but I have been happily blessed with such knowledge as a result of blindly refactoring some programming logic in a method that returns java.util.ArrayList. That it returns java.util.ArrayList I was not aware because my brain happily discarded the prefix Array from the return type leaving my eye to only perceive the word List. (To my non Java educated readers (are you out there?) I’m referring to how I missed half of a word on the declared type of a method, which for my non-programming crowd, is the most important piece of a method. A method is a chunk of code that can be… whatever! I’m off topic rambling!) In my refactoring (to my non-tech readers, refactoring means changing a program so that it does the same thing but reads a little better) I thought it would be really cool to change something like this:

        ArrayList classNames = new ArrayList();

//TODO: Add class name strings for my objects as they are developed classNames.add("com.ourpkg.SomeObject"); classNames.add("com.ourpkg.SomeOtherObject"); classNames.add("com.ourpkg.DougEFreshLyrics");

return classNames;

Into something like this:

//TODO: Add class name strings for my objects as they are developed
return Arrays.asList("com.ourpkg.SomeObject",
"com.ourpkg.SomeOtherObject",
"com.ourpkg.DougEFreshLyrics");

See? Less lines of code means easier to read and work on. It also means I’m smarter than you Mr. Original Writer of program so pay me more money please! The problem, as I stated earlier, was in the method declaration which read something like this:
protected ArrayList getHipHopsGreatestHits()
Mr. Original Writer of program clearly felt it was important to use an ArrayList but in my mind a list is a list is a list so my code should work just fine right? Well, after hours of debugging because we have this habit of catching Exception everywhere and pretending to tell Mrs. Log4J about the problem only when a flag is set in the particular class and passing an extra 385 options on the command line to tell our software exactly which logger to use and which classes are of interest and since that’s only done on Easter Sundays when Tom Brokaw is not on vacation for the nightly news, …after all of that I was able to find the problem. (Believe me, each one of the prior pre-conditions is relative for reasons I don’t know.) I found the problem of my overzealous refactoring. (My over excitement to change the program logic and make it easier so I can make more money, so I can buy a bigger TV set so I can watch 24 in High Def on Mondays, so I can…)

So now I ask the programming community, is this Mr. James Gosling’s fault? Should I blame Sun? How dare they create an inner-class of Arrays that implements the List interface but is clearly not an ArrayList! Don’t they realize that ArrayList is an interface??!! If I ever see a Sun engineer in the streets, man, it’s on! Watch your back Sun Microsystems, watch’a back…

*Followup
An astute reader might say, “Cliff you’re full of beans! What you’ve outlined is a compile time error not a runtime discrepancy that would be caught through a convoluted series of preconditions as you’ve described!” I would respond to the astute reader, “Stop reading in so much detail! Go out and get a girlfriend (or boyfriend?) instead of poking holes in my stories!” Honestly though the above description is a simplified version of the actual program which was indirectly invoked through reflection. Yes whenever something doesn’t quite make sense, blame reflection!

2 thoughts on “When is an ArrayList not an ArrayList?

  1. Actually, the requirements are different. The list returned by Arrays.asList() is read-only. This allows some optimizations to take place.

    As the javadoc says, “this method acts as bridge between array-based and collection-based APIs,” so you can’t really blame reflection.🙂

    That’s also why the List interface allows you to throw UnsupportedOperationException when calling List.add().

  2. Ok, if I can’t blame Sun and I can’t blame reflection then I blame YOU Tiago! I curse you for making Arrays.asList() return a read only structure! I curse you and the bridges and optimizations you rode in on! Really though, I should have known better than to make such an awkward change. Introducing not only a different type of List but also a reflective call is obivously not refactoring rather it’s re-coding. You know how you start off and convince yourself you’re only going to refactor a little here and tweak a little there and before you know it you’ve scalped the code bald!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s