Skip to content

18 数组 ​

数组和列表一样可按索引访问,但长度固定。

norm
main() {
  Array<Integer> scores = [3, 5, 8]
  scores[1] = 7
  printLine(scores[0])
  printLine(scores[1])
  printLine(scores.size())
  Integer total = scores[0] + scores[1] + scores[2]
  printLine(total)
  printLine(scores[2])
}

预期输出:

text
3
7
3
18
8

Array<Integer> 让字面量成为数组。给 scores[1] 赋值会改变该元素,size() 仍为三;若需要增减元素,应使用列表。

动手试试:替换第一个元素并再次输出。

详细规则:参考。

Norm 0.25