Dart 的List结构似乎不支持负索引。这背后的原因是什么?我曾经使用过的所有其他语言都支持这一点。为什么 dart 决定排除这样一个基本结构?
以下代码 -
void main() {
List<String> x = ["foo", "bar"];
print(x[-1]);
}
Run Code Online (Sandbox Code Playgroud)
产生——
Uncaught exception:
RangeError (index): Index out of range: index must not be negative: -1
Run Code Online (Sandbox Code Playgroud)
Dart 不支持负索引。
要访问相对于末尾的元素,您可以使用计算索引
print(x[x.length - 1])
Run Code Online (Sandbox Code Playgroud)
您可以在https://github.com/dart-lang/sdk 中创建功能请求以获取语言设计者的反馈。