ColdFusion 中数组的奇怪行为
Weird behaviour with arrays in ColdFusion
我遇到了一段代码的问题,稍作修改我得到了不同的结果,但事实并非如此。
版本 1 给出了正确的结果,我遇到了实际代码版本 2 的问题。
版本 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)>
版本 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)>
输出:
非常感谢任何建议!
我不是 Java 人,但据我所知...
在版本 1 中:keywords
包含 java.lang.String
个值,在版本 2 中:keywords
包含 java.lang.Double
个值。
在版本 2 中:toBeInactivated
包含要从双精度数组中删除的字符串值。
由于那些 Java 类型不匹配,它们将无法从集合中正确删除。我猜当 CF 传递底层对象数据时,它永远不会正确转换。老实说,当一切对 CF 来说都是无类型的时候,我希望如此。
从下面@Twillen 的评论中添加,这在您将 counter
转换为 java.lang.String
类型时有效:
<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">
ColdFusion 数组不是 Java 集合对象。所以我质疑对此类数组使用 removeAll() 和 retainAll() 。考虑到这一点,我的回答是:
<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">
我遇到了一段代码的问题,稍作修改我得到了不同的结果,但事实并非如此。
版本 1 给出了正确的结果,我遇到了实际代码版本 2 的问题。
版本 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)>
版本 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)>
输出:
非常感谢任何建议!
我不是 Java 人,但据我所知...
在版本 1 中:keywords
包含 java.lang.String
个值,在版本 2 中:keywords
包含 java.lang.Double
个值。
在版本 2 中:toBeInactivated
包含要从双精度数组中删除的字符串值。
由于那些 Java 类型不匹配,它们将无法从集合中正确删除。我猜当 CF 传递底层对象数据时,它永远不会正确转换。老实说,当一切对 CF 来说都是无类型的时候,我希望如此。
从下面@Twillen 的评论中添加,这在您将 counter
转换为 java.lang.String
类型时有效:
<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">
ColdFusion 数组不是 Java 集合对象。所以我质疑对此类数组使用 removeAll() 和 retainAll() 。考虑到这一点,我的回答是:
<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">