0Pricing
Mojo Academy · 课时

构建并扩展列表

向 List 追加、按索引访问并迭代

构建并扩展列表 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why a List?

When you need an ordered, growable group of values, reach for a List. It holds items in sequence and can stretch as you add. 📋

A Typed Container

A Mojo List is typed: every element shares one type, like List[Int]. That uniformity is what makes it fast.

var nums = List[Int]()

Start Empty

Calling List[Int]() gives you an empty list ready to fill. The type in brackets says what it will store.

var scores = List[Int]()

Append an Item

Add a value to the end with append. Each call grows the list by one, keeping insertion order.

scores.append(90)
scores.append(85)

Read by Index

Reach an element with square brackets and its position. Indexing is zero-based, so the first item is at 0.

var first = scores[0]

How Many Items

Ask a list its size with len. It returns the current count, which changes as you append or remove.

var n = len(scores)

Change an Element

Assign through an index to replace a value in place. The list stays the same length; only that slot updates.

scores[0] = 100

Iterate the Items

Walk a list with a for loop. It hands you each element directly, so you rarely manage indices yourself.

for s in scores:
    print(s)

Build From a Literal

You can fill a list at creation with a list literal. Mojo infers the element type from the values you give.

var fruits = List[String]("apple", "pear")

Remove From the End

Drop the last item with pop. It shrinks the list and hands back the value you removed.

var last = scores.pop()

Lists Grow on the Heap

A list stores its elements on the heap, so it can resize freely as you append. That flexibility is its superpower.

Quick Check

You have an empty List[Int] and call append three times. What is len of the list afterward?

Recap

A List is an ordered, typed, growable container. Use append to add, brackets to index, and len to count. 🎯

常见问题解答

「构建并扩展列表」课时是免费的吗?

是的 — 「构建并扩展列表」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。

「构建并扩展列表」这节课中我会学到什么?

向 List 追加、按索引访问并迭代 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Mojo Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「构建并扩展列表」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Mojo Academy 课中编写并运行代码吗?

能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 构建并扩展列表
  2. 使用 Dict 存储键值数据
  3. 使用 Set 存储唯一项
  4. 选择合适的集合
← 返回 Mojo Academy