Strange array behavior in ColdFusion

I ran into a problem with a piece of code, with a slight change. I get different results that should not be.

Version 1 gives the correct results, I am facing a problem with version 2, which is the actual code.

Version 1:

<cfset keywords = listToArray("1,2,3,4,5,6,7,8,9,10")> <!--- Common Code Below ---> <cfoutput>#getMetadata(keywords).getName()#</cfoutput> <cfset toBeAdded = keywords> <cfset toBeInactivated = listToArray("1,3,4,6,8")> <cfset toBeActivated = toBeInactivated> <cfset toBeAdded.removeAll(toBeInactivated)> <cfset toBeInactivated.removeAll(keywords)> <cfset toBeActivated.retainAll(keywords)> 

Version 2:

 <cfset keywords = []> <cfloop from="1" to="10" index="counter"> <cfset arrayAppend( keywords, counter )> </cfloop> <!--- If I add following line here then it is working as expected and similar to version 1: ---> <!--- <cfset keywords = listToArray(arrayToList(keywords))> ---> <!--- Common Code Below ---> <cfoutput>#getMetadata(keywords).getName()#</cfoutput> <cfset toBeAdded = keywords> <cfset toBeInactivated = listToArray("1,3,4,6,8")> <cfset toBeActivated = toBeInactivated> <cfset toBeAdded.removeAll(toBeInactivated)> <cfset toBeInactivated.removeAll(keywords)> <cfset toBeActivated.retainAll(keywords)> 

Outputs:

enter image description here enter image description here

Here's the gist: Version 1 and Version 2 .

Any suggestions are welcome!

+5
source share
2 answers

I am not a Java guy, but from what I can say ...

In version 1: keywords contains the values ​​of java.lang.String and in version 2: keywords contains the values ​​of java.lang.Double .

In version 2: toBeInactivated contains string values ​​that should be removed from the doubling array.

Since these Java types do not match, they will not be removed from the collection properly. I assume that when a CF transmits the underlying data of an object, it never throws correctly. Which, frankly, I expect when everything becomes impersonal to CF.

Adding to @Twillen's comment below, this works when you apply counter to the java.lang.String type:

 <cfset keywords = []> <cfloop from="1" to="10" index="counter"> <cfset arrayAppend( keywords, javaCast("string", counter) )> </cfloop> <!--- Common Code Below ---> <cfoutput>#getMetadata(keywords).getName()#</cfoutput> <cfset toBeAdded = keywords> <cfset toBeInactivated = listToArray("1,3,4,6,8")> <cfset toBeActivated = toBeInactivated> <cfset toBeAdded.removeAll(toBeInactivated)> <cfset toBeInactivated.removeAll(keywords)> <cfset toBeActivated.retainAll(keywords)> <cfdump var="#toBeAdded#" label="To Be Added"> <cfdump var="#toBeInactivated#" label="To Be Inactivated"> <cfdump var="#toBeActivated#" label="To Be Activated"> 
+6
source

The ColdFusion array is not a Java Collection object. So I ask a question about using removeAll () and preserveAll () with such arrays. Taking this into account, I reply:

 <cfset keywords = createobject("java","java.util.Vector").init()> <cfset toBeAdded = createobject("java","java.util.Vector").init()> <cfloop from="1" to="10" index="counter"> <cfset keywords.add(javaCast("int", counter))> <cfset toBeAdded.add(javaCast("int", counter))> </cfloop> <!--- Common Code Below ---> <cfoutput>#getMetadata(keywords).getName()#</cfoutput> <cfset listToBeInactivated = "1,3,4,6,8"> <cfset toBeInactivated = createobject("java","java.util.Vector").init()> <cfset toBeActivated = createobject("java","java.util.Vector").init()> <cfloop list="#listToBeInactivated#" index="index"> <cfset toBeInactivated.add(javaCast("int", index))> <cfset toBeActivated.add(javaCast("int", index))> </cfloop> <cfset toBeAdded.removeAll(toBeInactivated)> <cfset toBeInactivated.removeAll(keywords)> <cfset toBeActivated.retainAll(keywords)> <cfdump var="#toBeAdded#" label="toBeAdded"> <cfdump var="#toBeInactivated#" label="toBeInactivated"> <cfdump var="#toBeActivated#" label="toBeActivated"> 
0
source

Source: https://habr.com/ru/post/1269317/


All Articles