如何获取芭蕾舞女演员数组中对象的索引? [英] How to get the index of an object in Ballerina Array?

查看:56
本文介绍了如何获取芭蕾舞女演员数组中对象的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何有效地获取Ballerina数组中对象的索引? 有内置的功能吗?

How can I get the index of an object in a Ballerina array in an efficient way? Is there any inbuilt function to do that?

推荐答案

Ballerina现在提供

Ballerina now offers the indexOf and lastIndexOf methods, as of language specification 2020R1.

它们分别返回满足相等条件的项目的第一个和最后一个索引.如果找不到该值,则得到().

They return the first and last indices for items that satisfy the equality, respectively. We get () if the value is not found.

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
}

在芭蕾舞女演员游乐场中运行

Run it in the Ballerina Playground

可以在

The description of these and other functions can be found in the spec.

这篇关于如何获取芭蕾舞女演员数组中对象的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆