2/12/2009

{"X} + {"Y"} = {"X", "Y"}

Adding 2 Java arrays in 4 lines.

First, and foremost, do your damndest to avoid the use of primitives. If you're in the 21st century anyway, we have higher level languages and those languages have objects and, hot damn, let you make objects - so use them!!!

Be assured you'll hear more from me on that front for a long time to come. Anyhoo though...

This quick post is about when you, for one legit reason or another, do have to work with primitive Java arrays. As in "String[]" array, not "ArrayList<String>".

Today, Gil and I had to and were quite miffed to find there was no easy built in way to combine to existing arrays. Once the disgust was gone enough to type again, we coded a little utility to do it.

2 comments:

Hi there, got to your blog via Cory Haines' one...started reading your latest post, then worked my way down through the one on Killing Quality, then the TDD ones...quality stuff, then I hit this post and because I haven't been coding much lately, I had an itch to scratch, and thought I'd take up the challenge...I found that in JDK 1.6, the Arrays class now provides a 'copyOf' method that allows you to shorten your method (I made the method generic as well):

Prior Art

About MB

I dig bringing great things into the world, and sharing with other people. I've been doing various forms of bad-ass software for years, and these days I work for Industrial Logic as an XP coach/trainer, as well as write weekly for InfoQ. Frankly, I love doing this stuff, and love helping others love it too.
Twitter @ mbria