如何反转数组

发布于 2024-12-27 21:48:56 字数 107 浏览 2 评论 0 原文

您好,我有一个字符串数组,我需要将它们从最后一个字符串输出到第一个字符串。

我没有看到 arrayReverse() 函数,但我刚刚学习 ColdFusion

Hi I have an array of strings, and I need to output them from the last one to the first one.

I don't see an arrayReverse() function, but I'm only just learning ColdFusion

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

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

发布评论

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

评论(5

甜宝宝 2025-01-03 21:48:56

你可以反向循环数组,

<cfloop index="i" from="#arrayLen(myArray)#" to="1" step="-1">
   <cfoutput>#myArray[i]#</cfoutput>
</cfloop>

我认为你需要使用Java方法来真正反转数组。

<cfscript>
// and for those who use cfscript:
for ( var i = arrayLen( myArray ); i >= 1; i-- ) {
    writeOutput( myArray[i] );
}
</cfscript>

You can just loop over the array in reverse

<cfloop index="i" from="#arrayLen(myArray)#" to="1" step="-1">
   <cfoutput>#myArray[i]#</cfoutput>
</cfloop>

I think you need to use Java methods to really reverse the array.

<cfscript>
// and for those who use cfscript:
for ( var i = arrayLen( myArray ); i >= 1; i-- ) {
    writeOutput( myArray[i] );
}
</cfscript>
若无相欠,怎会相见 2025-01-03 21:48:56

仅供参考 CF 中的数组只是一个 ArrayList ,所以...

arr = [1,2,3];
createObject("java", "java.util.Collections").reverse(arr);
writeDump(arr);   // arr becomes [3,2,1]

我不会费心去写 arrayReverse() 因为数组是在 CF 中按值传递的(直到 CF2016 的 this.passArraybyReference)所以它超级效率低下。

FYI array in CF is just an ArrayList, so...

arr = [1,2,3];
createObject("java", "java.util.Collections").reverse(arr);
writeDump(arr);   // arr becomes [3,2,1]

And I would not bother writing arrayReverse() because array is passed by value in CF (not until CF2016's this.passArraybyReference) so it's super inefficient.

烙印 2025-01-03 21:48:56

我写了这个函数来反转数组。它修改数组并返回它。

function init(required array arr) {
    var arrLen = arrayLen(arr);
    for (var i = 1; i <= (arrLen / 2); i++) {
        var swap = arr[arrLen + 1 - i];
        arr[arrLen + 1 - i] = arr[i];
        arr[i] = swap;
    }
    return arr;
}

我已经测试过它,它适用于字符串数组以及对象等。

writeOutput(arrayReverse(['a','b','c']) ); // => ['c', 'b', 'a']

var a = ['apple', 'ball', 'cat', 'dog'];
arrayReverse(a);
writeOutput(a); // => ['dog', 'cat', 'ball', 'apple']

我将它放入它自己的组件中,因此更容易在不同的项目中使用。

I wrote this function to reverse an array. It modifies the array and returns it.

function init(required array arr) {
    var arrLen = arrayLen(arr);
    for (var i = 1; i <= (arrLen / 2); i++) {
        var swap = arr[arrLen + 1 - i];
        arr[arrLen + 1 - i] = arr[i];
        arr[i] = swap;
    }
    return arr;
}

I've tested it, and it works on arrays of strings, as well as objects, etc.

writeOutput(arrayReverse(['a','b','c']) ); // => ['c', 'b', 'a']

var a = ['apple', 'ball', 'cat', 'dog'];
arrayReverse(a);
writeOutput(a); // => ['dog', 'cat', 'ball', 'apple']

I put it into it's own component, so it's easier to use in different projects.

我恋#小黄人 2025-01-03 21:48:56

哦,但是有一个 ArraySort 方法!

ArraySort( array, sort_type [, sort_order] );

返回布尔值。

array 通过引用进行更新。

sort_type 可以是 numerictexttextnocase

sort_order 可以是 asc desc

<cfscript>
test = [ "c", "d", "a", "b" ];
arraySort( test, 'textnocase' );

test is now:
[ "a", "b", "c", "d" ]

</cfscript>

请查看此处的文档:

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-ab/arraysort.html

Oh but there is an ArraySort method!

ArraySort( array, sort_type [, sort_order] );

Returns boolean.

array is updated by reference.

sort_type can be numeric, text or textnocase

sort_order can be asc or desc

<cfscript>
test = [ "c", "d", "a", "b" ];
arraySort( test, 'textnocase' );

test is now:
[ "a", "b", "c", "d" ]

</cfscript>

Check out the documentation here:

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-a-b/arraysort.html

时光暖心i 2025-01-03 21:48:56
<cfscript>
    test = [ "a", "b", "c", "d" ];
    writeDump(listToArray(reverse(arrayToList(test))));
</cfscript>

会成功的。

<cfscript>
    test = [ "a", "b", "c", "d" ];
    writeDump(listToArray(reverse(arrayToList(test))));
</cfscript>

Will do the trick.

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