ColdFusion ORM Book

Merging two arrays

I've mentioned this before in passing in previous posts, but I've just used it to solve a problem and thought it deserved it's own blog post!

The problem I had was that I had two arrays that I want to merge into one. ColdFusion doesn't have an in-built function for merging arrays so you end up having to loop and append one element at a time, which is frankly rubbish! Here's an example:

<cfset foo = [1,2,3]>
<cfset bar = [4,5,6]>
<cfloop array="#bar#" index="i">
 <cfset ArrayAppend( foo, i )>

As we all know, ColdFusion is built on-top of Java, so we can take advantage of this and use some of the Java methods. In short we can get rid of the loop and just do this instead:

<cfset foo = [1,2,3]>
<cfset bar = [4,5,6]>
<cfset foo.addAll( bar )>

Much nicer!


  1. Is there a like a quickdocs guide anywhere to all the array/collection java stuff we can access that easily?

    Comment by brian – June 09, 2010
  2. @Brian, I don't think so, this partly because things like this aren't "official" as Adobe might choose in the future to use a different Java class for arrays which doesn't have the addAll method. This is unlikely to happen, but it could!

    Comment by John Whish – June 09, 2010
  3. Just for a chuckle :-p

    foo = [1,2,3];
    bar = [4,5,6];
    foo = ListToArray(ListAppend(ArrayToList(foo),ArrayToList(bar)));

    Comment by Steve Glachan – June 09, 2010
  4. There are always compatibility issues between CF releases - so that's no reason to not use a useful Java function on the offchance that maybe it might go away, since you should be regression testing against any new release anyway!

    For this specific case... well, the odds of arrays being re-done to not implement the Collections interface is probably zero. I guess the specific implementation might somehow become unmodifiable under the covers, but I'd doubt that too.
    ( Info at: )

    However, if you're not happy with minute risk that .addAll might go away, why not take a look at Railo, which has had the ArrayMerge CFML function since v1. :)

    Comment by Peter Boughton – June 09, 2010
  5. @Steve, haha - I concede, you can do it in pure CFML!

    @Peter, what's this "testing" you refer to? :) I agree with you that the small risk, is outweighed by the benefits and Railo is very cool!

    Comment by John Whish – June 09, 2010
  6. Here is some java functionality for structs blogged by Ben Nadel

    Comment by brian – June 09, 2010
  7. As a side note, when you google for Java features of the core ColdFusion objects, make sure to look for "java 6", not "java 2" since the latest version (6) does have some new stuff available.... I don't know much about it, but I know the pattern library has been updated in v6, which is what CF8+ ships with (I believe).

    Comment by Ben Nadel – June 10, 2010

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments


Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)


Recent Posts