Skip to main content
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Jared Rypka-Hauer
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Jared Rypka-Hauer ( @ArmchairDeity )

Caution: Java String::Split() Does Not Create A ColdFusion Array

By on
Tags:

This has been covered before, but you can call the Java string methods on ColdFusion string objects. One of those methods is the String::Split() method. In the Java documentation, it says that it splits this string around matches of the given regular expression. The return value is an array of String objects. When dumped out, these arrays look just like ColdFusion arrays, but they are NOT ColdFusion arrays. ColdFusion arrays are actually Vectors.

To demonstrate that, here are two arrays, one built by ColdFusion and one built by Java:

<!--- Set up the words list. --->
<cfset strWords = "I like to move it, move it!" />

<!---
	Create the words array using the underlying
	Java method, String::Split().
--->
<cfset arrWordsJava = strWords.Split( " {1}" ) />


<!---
	Create the words array using the ColdFusion
	function ListToArray().
--->
<cfset arrWordsCF = ListToArray( strWords, " " ) />

Dumping out the Java String::Split() array, we see:

CFDump Java String::Split()

Dumping out the ColdFusion ListToArray() array, we see:

CFDump ColdFusion ListToArray()

They look exactly the same, right??? Maybe, but they are quite different. From what I can see, the Array returned by the Split() method is read only. If you look at the class methods:

CFDump - Java String::Split() Java Class Methods

... you can see there are no methods about altering it. In fact, if you try to use ArrayResize() on this array, you get the error:

null null

... and if you try to update one of the values (as in arrWordsJava[ 1 ] = "Ben") you get the error:

You have attempted to dereference a scalar variable of type class coldfusion.runtime.Cast$1 as a structure with members.

Seems like this is read only. The Split() method is really awesome, especially since it splits on a Regular Expression, but just BE CAREFUL, the resultant array is pretty much only for reference.

Want to use code from this post? Check out the license.

Reader Comments

48 Comments

I came acrossed this _exact_ issue yesterday. This explains it.

Consider the following code:

<cfset tzObj=createObject("java","java.util.TimeZone")>
<cfset tzs = tzObj.getAvailableIDs()>
<cfdump var="#tzs#">

<!--- uncomment the next line and you'll get the funky error --->
<!--- <cfset arraySort(tzs, "textnocase")> --->

<!--- rebuild the array manually and the arraySort() works....WEIRD! --->
<cfset foo = arrayNew(1)>
<cfloop from="1" to="#arrayLen(tzs)#" index="i">
<cfset arrayAppend(foo, tzs[i])>
</cfloop>
<!--- no error here --->
<cfset arraySort(foo, "textnocase")>
<cfdump var="#foo#">

So why to some of the array functions work? For example, arrayToList(tzs) works. I ultimately used arrayToList() and listSort() to achieve the desired result.

PS. Noob question - how did you dump the class methods?

15,674 Comments

Todd,

Yeah, what you are saying makes sense. ArrayToList() must actually iterate over the array to get the value (hence it works with the read-only properties of the array). I guess it is not doing any kind of Join or anything like that.

As far as getting the class methods, I use reflection to get the information about the Java object (works also with ColdFusion objects). Take a look at the ColdFusion User Defined Function I wrote for this:

bennadel.com/index.cfm?dax=blog:206.view

It works 95% of the time. I have to add a try/catch in there for special cases, but so far it's been pretty great.

1 Comments

I know this blog is old, but arent coldfusion arrays just java lists? I dont think coldfusion arrays are really arrays.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel