Repository navigation
Collection Builtins
本章是速查表。容器详细用法在 [List / Set / Map] 和 [Stack / Queue]。
This is a cheat sheet. Full usage is in [List / Set / Map] and [Stack / Queue].
vector() // 空列表
set() // 空集合
map() // 空映射
stack() // 空栈
queue() // 空队列
创建函数没有参数。初始内容通过 .add(...) 添加。
Creation functions take no arguments. Add content with .add(...).
| 方法 | 说明 |
|---|---|
add(x) |
追加 |
get(i) |
按下标读 |
remove(i) |
按下标删,返回被删元素 |
remove(x) |
按值删,返回是否成功 |
contains(x) |
是否包含 |
size() |
长度 |
isEmpty() |
是否为空 |
| 方法 | 说明 |
|---|---|
add(x) |
添加(重复无效) |
remove(x) |
移除 |
contains(x) |
是否包含 |
size() |
元素个数 |
isEmpty() |
是否为空 |
| 方法 | 说明 |
|---|---|
put(k, v) |
写入 |
get(k) |
读取(不存在返回 null) |
remove(k) |
移除 |
containsKey(k) |
键是否存在 |
isEmpty() |
是否为空 |
| 方法 | 说明 |
|---|---|
push(x) |
入栈 |
pop() |
出栈 |
peek() |
看栈顶 |
isEmpty() |
是否为空 |
| 方法 | 说明 |
|---|---|
offer(x) |
入队 |
poll() |
出队 |
peek() |
看队首 |
isEmpty() |
是否为空 |
容器方法也支持函数形式,参数把容器放在第一位:
Container methods also support a function form with the container first:
add(list, x) // 等价 list.add(x)
get(map, key) // 等价 map.get(key)
put(map, key, value) // 等价 map.put(key, value)
size(list) // 等价 list.size()
contains(set, x) // 等价 set.contains(x)
这种写法不推荐——它和"方法属于对象"的直觉不符。新代码统一用方法形式。
Not recommended — it fights the intuition that methods belong to objects. New code uses the method form.
-
默认用
vector。 -
Set 没有下标访问。
-
Map 没有
size()——只有isEmpty()。 -
Stack 用
push/pop;Queue 用offer/poll。 -
空容器上
pop/poll/peek会报错。 -
Default to
vector. -
Sets have no indexed access.
-
Maps have no
size()— onlyisEmpty(). -
Stacks use
push/pop; queues useoffer/poll. -
pop/poll/peekon empty containers errors.
-
五种创建函数:
vector/set/map/stack/queue。 -
方法形式优先,函数形式兼容。
-
具体用法见数据结构部分。
-
Five creation functions:
vector/set/map/stack/queue. -
Prefer method form; function form is legacy.
-
See the Data Structures section for details.
下一章 [文件 I/O / File-IO] 讲怎么读写文件。
Next, [文件 I/O / File-IO] covers reading and writing files.
- 首页 / Home
- 安装与快速开始 / Installation & Quick Start
- 第一个脚本 / Your First Script
- 命令速查 / Command Reference
- 自动执行脚本 / Auto-run Scripts
- 定义函数 / Defining Functions
- 函数重载 / Function Overloading
- Lambda 表达式 / Lambda Expressions
- 函数式接口 / Functional Interfaces
- 选择器总览 / Selectors Overview
- 选择器过滤器 / Selector Filters
- EntityRef 引用 / EntityRef
- 占位符参考 / Placeholders Reference
- PlaceholderAPI 桥接 / PAPI Bridge
- 创建 GUI / Creating a GUI
- GUISlot 槽位 / GUISlot
- 点击回调 / Click Callbacks
- onClose 回调 / onClose Callback
- GUI 刷新 / GUI Refresh
- 数学函数 / Math Built-ins
- 高精度计算 / BigDecimal Built-ins
- 字符串函数 / String Built-ins
- 集合函数 / Collection Built-ins
- 文件 I/O / File I/O
- 命令与 sleep / Commands & sleep