Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Merging two arrays

DZone's Guide to

Merging two arrays

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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 )>
</cfloop>

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!




Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}