为什么 ListAppend 是非破坏性的,而 ArrayAppend 和 StructInsert 都是破坏性的?

发布于 2024-12-20 13:56:55 字数 411 浏览 1 评论 0原文

我刚刚花了将近一个小时试图找出一个总是返回空字符串的列表的问题。我使用 ListAppend 就像使用 ArrayAppend 或 StructInsert 一样,但显然 ListAppend 的工作方式不同。 ListAppend 的工作方式与其他方式不同的原因是什么(如果有的话)?

<cfset ListAppend(list, item)>

列表 = ''

<cfset ArrayAppend(array, item)>

数组[1] = 项目

<cfset StructInsert(struct, 'key', item)>

struct.key = 项目

I just spent almost an hour trying to figure out an issue with having a list that would always return an empty string. I was using ListAppend just like one uses ArrayAppend or StructInsert, but apparently ListAppend works differently. What, if any, is the reasoning behind having ListAppend work differently from everything else?

<cfset ListAppend(list, item)>

list = ''

<cfset ArrayAppend(array, item)>

array[1] = item

<cfset StructInsert(struct, 'key', item)>

struct.key = item

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-12-27 13:56:55

可能是因为列表只是一个大的字符串。与数组和结构不同,字符串是不可变的,这意味着它们不能更改。要“附加”新值,您需要创建一个全新的字符串。数组和结构体是可变的。所以你可以“就地”修改它们。

Possibly because a list is just a big String. Unlike arrays and structures, Strings are immutable, meaning they cannot be changed. To "append" a new value, you need to create an entirely new String. Arrays and structures are mutable. So you can modify them "in place".

三岁铭 2024-12-27 13:56:55

ColdFusion 中的列表只是字符串,而 ColdFusion(和 Java)中的字符串是不可变的。它们无法改变。因此 ListAppend() 必须返回带有值的新字符串,而不是修改现有字符串。

<cfset newList = listAppend(oldList, "New Value") />

Lists in ColdFusion are just Strings and strings in ColdFusion (and Java) are immutable. They cannot be changed. So ListAppend() must return the a new string with the value instead of modifying the existing string.

<cfset newList = listAppend(oldList, "New Value") />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文