如何有效地获取 Ballerina 数组中对象的索引?
有没有内置函数可以做到这一点?
芭蕾舞演员现在提供indexOf https://github.com/ballerina-platform/ballerina-lang/blob/56c946b28cb7617974f4c290970fcd67505ab0ac/langlib/lang.array/src/main/java/org/ballerinalang/langlib/array/IndexOf.java#L47 and lastIndexOf https://github.com/ballerina-platform/ballerina-lang/blob/56c946b28cb7617974f4c290970fcd67505ab0ac/langlib/lang.array/src/main/java/org/ballerinalang/langlib/array/LastIndexOf.java#L45方法,截至语言规范 2020R1。
它们分别返回满足相等性的项目的第一个和最后一个索引。我们得到()
如果没有找到该值。
import ballerina/io;
public function main() {
string[*] example = ["this", "is", "an", "example", "for", "example"];
// indexOf returns the index of the first element found
io:println(example.indexOf("example")); // 3
// The second parameter can be used to change the starting point
// Here, "is" appears at index 1, so the return value is ()
io:println(example.indexOf("is", 3) == ()); // true
// lastIndexOf will find the last element instead
// (the implementation will do the lookup backwards)
io:println(example.lastIndexOf("example")); // 5
// Here the second parameter is where to stop looking
// (or where to start searching backwards from)
io:println(example.lastIndexOf("example", 4)); // 3
}
这些和其他功能的描述可以找到在规范中 https://github.com/ballerina-platform/ballerina-spec/blob/master/lang/lib/array.bal.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)