azl397985856 / fe-interview

宇宙最强的前端面试指南 (https://lucifer.ren/fe-interview)
Apache License 2.0
2.83k stars 260 forks source link

【每日一题】- 2021-07-12 数组的索引为什么从 0 开始?而不是从 1 开始? #158

Closed azl397985856 closed 2 years ago

Talent-Rain commented 3 years ago
azl397985856 commented 3 years ago
  1. 索引的含义是偏移量(offset),偏移量从 0 开始,因此索引也从 0 开始。
  2. 数组需要支持随机访问,一种实现方式就是通过 *基值+偏移量每项的字节数** 访问任意索引位置。如果索引从 1 开始,那么偏移量就等于索引-1,这样会多一次减法运算。
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.