gpt4 book ai didi

dart - Dart 列表否定索引不起作用

转载 作者:行者123 更新时间:2023-12-03 03:10:24 25 4
gpt4 key购买 nike

Dart的List结构似乎不支持负索引。这背后的原因是什么?我曾经使用的所有其他语言都支持此功能。为什么dart决定排除这种基本结构?

以下代码-

void main() {
List<String> x = ["foo", "bar"];
print(x[-1]);
}

产生-
Uncaught exception:
RangeError (index): Index out of range: index must not be negative: -1

最佳答案

Dart不支持负索引。

要访问相对于末尾的元素,您可以使用

print(x[x.length - 1])

您可以在 https://github.com/dart-lang/sdk中创建功能请求,以获得语言设计者的反馈。

关于dart - Dart 列表否定索引不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54278408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com