:github_url: hide .. _class_Array: Array ===== 一种内置数据结构,包含一系列元素。 .. rst-class:: classref-introduction-group 描述 ---- 数组数据类型,默认可以包含任意 :ref:`Variant` 类型元素的序列。创建\ *类型化数组*\ 可以将其中的值限制为特定的类型。可以使用从 ``0`` 开始的索引号来访问元素。可以使用负的索引号从结尾开始计数(\ ``-1`` 表示倒数第一个元素、\ ``-2`` 表示倒数第二个元素,以此类推)。 .. tabs:: .. code-tab:: gdscript var array = ["第一", 2, 3, "最后"] print(array[0]) # 输出“第一” print(array[2]) # 输出 3 print(array[-1]) # 输出“最后” array[1] = "第二" print(array[1]) # 输出“第二” print(array[-3]) # 输出“第二” # 这个类型化数组中只能包含整数。 # 尝试向其中添加其他类型会报错。 var typed_array: Array[int] = [1, 2, 3] .. code-tab:: csharp Godot.Collections.Array array = ["第一", 2, 3, "最后"]; GD.Print(array[0]); // 输出“开头” GD.Print(array[2]); // 输出 3 GD.Print(array[^1]); // 输出“最后” array[2] = "第二"; GD.Print(array[1]); // 输出“第二” GD.Print(array[^3]); // 输出“第二” // 这个类型化数组中只能包含整数。 // 尝试向其中添加其他类型会报错。 Godot.Collections.Array typedArray = [1, 2, 3]; \ **注意:**\ 数组始终按\ **引用**\ 传递。如果要获取数组的副本,让改动独立于原始数组,请使用 :ref:`duplicate()`\ 。 \ **注意:**\ **不支持**\ 在遍历数组元素时擦除元素,这样做可能造成预料之外的行为。 \ **紧缩数组、类型数组、无类型数组:**\ 紧缩数组在遍历和修改时通常比同类型的类型数组要快(例如将 :ref:`PackedInt64Array` 和 ``Array[int]`` 相比)。紧缩数组占据的内存也相对较少。但紧缩数组的缺点是不够灵活,因为没有提供 :ref:`map()` 之类的便捷方法。相应地,类型数组在遍历和修改时要比无类型数组要快。 .. note:: 通过 C# 使用该 API 时会有显著不同,详见 :ref:`doc_c_sharp_differences`\ 。 .. rst-class:: classref-reftable-group 构造函数 -------- .. table:: :widths: auto +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ base\: :ref:`Array`, type\: :ref:`int`, class_name\: :ref:`StringName`, script\: :ref:`Variant`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedByteArray`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedColorArray`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedFloat32Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedFloat64Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedInt32Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedInt64Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedStringArray`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedVector2Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedVector3Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`Array`\ (\ from\: :ref:`PackedVector4Array`\ ) | +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`all`\ (\ method\: :ref:`Callable`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`any`\ (\ method\: :ref:`Callable`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`append`\ (\ value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`append_array`\ (\ array\: :ref:`Array`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`assign`\ (\ array\: :ref:`Array`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`back`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`bsearch`\ (\ value\: :ref:`Variant`, before\: :ref:`bool` = true\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`bsearch_custom`\ (\ value\: :ref:`Variant`, func\: :ref:`Callable`, before\: :ref:`bool` = true\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`count`\ (\ value\: :ref:`Variant`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`duplicate`\ (\ deep\: :ref:`bool` = false\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`duplicate_deep`\ (\ deep_subresources_mode\: :ref:`int` = 1\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`erase`\ (\ value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`fill`\ (\ value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`filter`\ (\ method\: :ref:`Callable`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`find`\ (\ what\: :ref:`Variant`, from\: :ref:`int` = 0\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`find_custom`\ (\ method\: :ref:`Callable`, from\: :ref:`int` = 0\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`front`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`get`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_typed_builtin`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`StringName` | :ref:`get_typed_class_name`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`get_typed_script`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has`\ (\ value\: :ref:`Variant`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`hash`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`insert`\ (\ position\: :ref:`int`, value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_empty`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_read_only`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_same_typed`\ (\ array\: :ref:`Array`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_typed`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`make_read_only`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`map`\ (\ method\: :ref:`Callable`\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`max`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`min`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`pick_random`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`pop_at`\ (\ position\: :ref:`int`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`pop_back`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`pop_front`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`push_back`\ (\ value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`push_front`\ (\ value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`reduce`\ (\ method\: :ref:`Callable`, accum\: :ref:`Variant` = null\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_at`\ (\ position\: :ref:`int`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`resize`\ (\ size\: :ref:`int`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`reverse`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`rfind`\ (\ what\: :ref:`Variant`, from\: :ref:`int` = -1\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`rfind_custom`\ (\ method\: :ref:`Callable`, from\: :ref:`int` = -1\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set`\ (\ index\: :ref:`int`, value\: :ref:`Variant`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`shuffle`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`size`\ (\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`slice`\ (\ begin\: :ref:`int`, end\: :ref:`int` = 2147483647, step\: :ref:`int` = 1, deep\: :ref:`bool` = false\ ) |const| | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`sort`\ (\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`sort_custom`\ (\ func\: :ref:`Callable`\ ) | +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group 运算符 ------ .. table:: :widths: auto +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator !=`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`Array` | :ref:`operator +`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator \<`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator \<=`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator ==`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator >`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator >=`\ (\ right\: :ref:`Array`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`operator []`\ (\ index\: :ref:`int`\ ) | +-------------------------------+----------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 构造函数说明 ------------ .. _class_Array_constructor_Array: .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ ) :ref:`🔗` 构造空的 **Array**\ 。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ base\: :ref:`Array`, type\: :ref:`int`, class_name\: :ref:`StringName`, script\: :ref:`Variant`\ ) 根据 ``base`` 数组创建类型化的数组。类型化的数组只能包含给定类型的元素,或者从给定类继承的元素,构造函数的参数如下所述: - ``type`` 是内置 :ref:`Variant` 类型,是一个 :ref:`Variant.Type` 常量。 - ``class_name`` 是内置类名(见 :ref:`Object.get_class()`\ )。 - ``script`` 是关联的脚本。它必须是 :ref:`Script` 实例或 ``null``\ 。 如果 ``type`` 不是 :ref:`@GlobalScope.TYPE_OBJECT`\ ,则 ``class_name`` 必须为空的 :ref:`StringName`\ ,且 ``script`` 必须为 ``null``\ 。 :: class_name Sword extends Node class Stats: pass func _ready(): var a = Array([], TYPE_INT, "", null) # Array[int] var b = Array([], TYPE_OBJECT, "Node", null) # Array[Node] var c = Array([], TYPE_OBJECT, "Node", Sword) # Array[Sword] var d = Array([], TYPE_OBJECT, "RefCounted", Stats) # Array[Stats] \ ``base`` 数组的元素在必要时进行转换。如果无法转换或 ``base`` 已被类型化,则该构造函数失败并返回一个空的 **Array**\ 。 在 GDScript 中,这个构造函数通常不是必需的,因为可以通过静态类型创建类型化的数组: :: var numbers: Array[float] = [] var children: Array[Node] = [$Node, $Sprite2D, $RigidBody3D] var integers: Array[int] = [0.2, 4.5, -2.0] print(integers) # 输出 [0, 4, -2] .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`Array`\ ) 返回与 ``from`` 相同的数组。如果你需要一个数组的副本,请使用 :ref:`duplicate()`\ 。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedByteArray`\ ) 从 :ref:`PackedByteArray` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedColorArray`\ ) 从 :ref:`PackedColorArray` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedFloat32Array`\ ) 从 :ref:`PackedFloat32Array` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedFloat64Array`\ ) 从 :ref:`PackedFloat64Array` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedInt32Array`\ ) 从 :ref:`PackedInt32Array` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedInt64Array`\ ) 从 :ref:`PackedInt64Array` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedStringArray`\ ) 从 :ref:`PackedStringArray` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedVector2Array`\ ) 从 :ref:`PackedVector2Array` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedVector3Array`\ ) 从 :ref:`PackedVector3Array` 构造一个数组。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Array` **Array**\ (\ from\: :ref:`PackedVector4Array`\ ) 从 :ref:`PackedVector4Array` 构造一个数组。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法说明 -------- .. _class_Array_method_all: .. rst-class:: classref-method :ref:`bool` **all**\ (\ method\: :ref:`Callable`\ ) |const| :ref:`🔗` 对数组中的每个元素调用给定的 :ref:`Callable`\ ,如果 :ref:`Callable` 为数组中的 *所有* 元素返回 ``true``\ ,则返回 ``true``\ 。如果 :ref:`Callable` 为一个或多个数组元素返回 ``false``\ ,则此方法返回 ``false``\ 。 该 ``method`` 应采用一个 :ref:`Variant` 参数(当前数组元素)并返回一个 :ref:`bool`\ 。 .. tabs:: .. code-tab:: gdscript func greater_than_5(number): return number > 5 func _ready(): print([6, 10, 6].all(greater_than_5)) # 输出 true (3/3 元素被评估为真)。 print([4, 10, 4].all(greater_than_5)) # 输出 false (1/3 元素被评估为真)。 print([4, 4, 4].all(greater_than_5)) # 输出 false (0/3 元素被评估为真)。 print([].all(greater_than_5)) # 输出 true (0/0 元素被评估为真)。 # 与上面的第一行相同,但使用 lambda 函数。 print([6, 10, 6].all(func(element): return element > 5)) # 输出 true .. code-tab:: csharp private static bool GreaterThan5(int number) { return number > 5; } public override void _Ready() { // 输出 True (3/3 元素被评估为真)。 GD.Print(new Godot.Collections.Array>int< { 6, 10, 6 }.All(GreaterThan5)); // 输出 False (1/3 元素被评估为真)。 GD.Print(new Godot.Collections.Array>int< { 4, 10, 4 }.All(GreaterThan5)); // 输出 False (0/3 元素被评估为真)。 GD.Print(new Godot.Collections.Array>int< { 4, 4, 4 }.All(GreaterThan5)); // 输出 True (0/0 元素被评估为真)。 GD.Print(new Godot.Collections.Array>int< { }.All(GreaterThan5)); // 与上面的第一行相同,但使用 lambda 函数。 GD.Print(new Godot.Collections.Array>int< { 6, 10, 6 }.All(element => element > 5)); // 输出 True } 另请参见 :ref:`any()`\ 、\ :ref:`filter()`\ 、\ :ref:`map()` 和 :ref:`reduce()`\ 。 \ **注意:**\ 与依赖 :ref:`filter()` 返回的数组大小不同,此方法会尽可能早地返回以提高性能(尤其是对于大型数组)。 \ **注意:**\ 对于空数组,此方法 `总是 `__ 返回 ``true``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_any: .. rst-class:: classref-method :ref:`bool` **any**\ (\ method\: :ref:`Callable`\ ) |const| :ref:`🔗` 对数组中的每个元素调用给定的 :ref:`Callable`\ ,如果 :ref:`Callable` 为数组中的\ * 一个或多个*\ 元素返回 ``true``\ ,则返回 ``true``\ 。如果 :ref:`Callable` 为数组中的所有元素返回 ``false``\ ,则该方法返回 ``false``\ 。 该 ``method`` 应接受一个 :ref:`Variant` 参数(当前数组元素)并返回一个 :ref:`bool`\ 。 :: func greater_than_5(number): return number > 5 func _ready(): print([6, 10, 6].any(greater_than_5)) # 输出 true (3 个元素被评估为真)。 print([4, 10, 4].any(greater_than_5)) #输出 true (1 个元素被评估为真)。 print([4, 4, 4].any(greater_than_5)) # 输出 false (0 个元素被评估为真)。 print([].any(greater_than_5)) # 输出 false (0 个元素被评估为真)。 # 与上面的第一行相同,但使用 lambda 函数。 print([6, 10, 6].any(func(number): return number > 5)) # 输出 true 另见 :ref:`all()`\ 、\ :ref:`filter()`\ 、\ :ref:`map()` 和 :ref:`reduce()`\ 。 \ **注意:**\ 与依赖 :ref:`filter()` 返回的数组大小不同,此方法会尽可能早地返回以提高性能(尤其是对于大型数组)。 \ **注意:**\ 对于一个空数组,这个方法总是返回 ``false``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_append: .. rst-class:: classref-method |void| **append**\ (\ value\: :ref:`Variant`\ ) :ref:`🔗` 将 ``value`` 追加到数组末尾(\ :ref:`push_back()` 的别名)。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_append_array: .. rst-class:: classref-method |void| **append_array**\ (\ array\: :ref:`Array`\ ) :ref:`🔗` 在该数组的末尾追加其他 ``array``\ 。 :: var numbers = [1, 2, 3] var extra = [4, 5, 6] numbers.append_array(extra) print(numbers) # 输出 [1, 2, 3, 4, 5, 6] .. rst-class:: classref-item-separator ---- .. _class_Array_method_assign: .. rst-class:: classref-method |void| **assign**\ (\ array\: :ref:`Array`\ ) :ref:`🔗` 将另一个 ``array`` 的元素赋值到该数组中。调整数组大小以匹配 ``array``\ 。如果数组是有类型的,则执行类型转换。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_back: .. rst-class:: classref-method :ref:`Variant` **back**\ (\ ) |const| :ref:`🔗` 返回数组的最后一个元素。如果数组为空,则失败并返回 ``null``\ 。另见 :ref:`front()`\ 。 \ **注意:**\ 与 ``[]`` 运算符(\ ``array[-1]``\ )不同,错误生成时不会停止项目执行。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_bsearch: .. rst-class:: classref-method :ref:`int` **bsearch**\ (\ value\: :ref:`Variant`, before\: :ref:`bool` = true\ ) |const| :ref:`🔗` 返回已排序数组中 ``value`` 的索引。如果找不到,则返回应被插入 ``value`` 的位置以保持数组被排序。使用的算法是\ `二分查找算法 `__\ 。 如果 ``before`` 为 ``true``\ (默认情况下),则返回的索引位于数组中所有等于 ``value`` 的已有元素之前。 :: var numbers = [2, 4, 8, 10] var idx = numbers.bsearch(7) numbers.insert(idx, 7) print(numbers) # 输出 [2, 4, 7, 8, 10] var fruits = ["Apple", "Lemon", "Lemon", "Orange"] print(fruits.bsearch("Lemon", true)) # 输出 1,位于第一个 "Lemon"。 print(fruits.bsearch("Lemon", false)) # 输出 3,位于 "Orange"。 \ **注意:**\ 对\ *未排序的*\ 数组调用 :ref:`bsearch()` 将导致意外行为。调用该方法之前,请使用 :ref:`sort()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_bsearch_custom: .. rst-class:: classref-method :ref:`int` **bsearch_custom**\ (\ value\: :ref:`Variant`, func\: :ref:`Callable`, before\: :ref:`bool` = true\ ) |const| :ref:`🔗` 返回已排序数组中 ``value`` 的索引。如果找不到,则返回 ``value`` 应插入的位置,以保持数组已排序(使用 ``func`` 进行比较)。使用的算法是\ `二分查找算法 `__\ 。 与 :ref:`sort_custom()` 类似,\ ``func`` 会根据需要多次调用,接收一个数组元素和 ``value`` 作为参数。如果数组元素应该在 ``value`` *后面*\ ,则函数应该返回 ``true``\ ,否则应该返回 ``false``\ 。 如果 ``before`` 为 ``true``\ (默认情况下),则返回的索引位于数组中所有等于 ``value`` 的已有元素之前。 :: func sort_by_amount(a, b): if a[1] < b[1]: return true return false func _ready(): var my_items = [["Tomato", 2], ["Kiwi", 5], ["Rice", 9]] var apple = ["Apple", 5] # "Apple" 被插入在 "Kiwi" 之前。 my_items.insert(my_items.bsearch_custom(apple, sort_by_amount, true), apple) var banana = ["Banana", 5] # "Banana" 被插入在 "Kiwi" 之后。 my_items.insert(my_items.bsearch_custom(banana, sort_by_amount, false), banana) # 输出 [["Tomato", 2], ["Apple", 5], ["Kiwi", 5], ["Banana", 5], ["Rice", 9]] print(my_items) \ **注意:**\ 在\ *未排序的*\ 数组上调用 :ref:`bsearch_custom()` 将导致意外行为。在调用该方法之前,请将 :ref:`sort_custom()` 与 ``func`` 结合使用。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_clear: .. rst-class:: classref-method |void| **clear**\ (\ ) :ref:`🔗` 从该数组中移除所有元素。相当于调用 :ref:`resize()` 时指定大小为 ``0``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_count: .. rst-class:: classref-method :ref:`int` **count**\ (\ value\: :ref:`Variant`\ ) |const| :ref:`🔗` 返回数组中某个元素出现的次数。 要计算某个数组中有多少元素满足某个条件,参见\ :ref:`reduce()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_duplicate: .. rst-class:: classref-method :ref:`Array` **duplicate**\ (\ deep\: :ref:`bool` = false\ ) |const| :ref:`🔗` 返回数组的新副本。 默认情况下返回的是\ **浅拷贝**\ :嵌套的 **Array**\ 、\ :ref:`Dictionary` 和 :ref:`Resource` 元素与原数组共享。对这些元素的修改会影响另一个数组。 如果 ``deep`` 为 ``true`` 则会返回\ **深拷贝**\ :嵌套的数组和字典也会进行(递归的)复制。不过 :ref:`Resource` 仍然是和原数组共享的。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_duplicate_deep: .. rst-class:: classref-method :ref:`Array` **duplicate_deep**\ (\ deep_subresources_mode\: :ref:`int` = 1\ ) |const| :ref:`🔗` 深度复制该数组,类似 :ref:`duplicate()`\ ``(true)``\ ,但能够额外控制子资源的处理方式。 \ ``deep_subresources_mode`` 必须是 :ref:`DeepDuplicateMode` 中的一个值。默认只会(递归)复制内部资源。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_erase: .. rst-class:: classref-method |void| **erase**\ (\ value\: :ref:`Variant`\ ) :ref:`🔗` 查找并从数组中移除 ``value`` 的第一个匹配值。如果数组中不存在 ``value``\ ,则什么也不会发生。要通过索引移除元素,请改用 :ref:`remove_at()`\ 。 \ **注意:**\ 该方法将移除的 ``value`` 后每个元素的索引移回一位,这可能会产生明显的性能成本,尤其是在较大的数组上。 \ **注意:**\ 在迭代数组时移除元素\ **不**\ 受支持,并且将导致不可预测的行为。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_fill: .. rst-class:: classref-method |void| **fill**\ (\ value\: :ref:`Variant`\ ) :ref:`🔗` 将该数组中的所有元素都设置为给定的 ``value``\ 。 该方法通常与 :ref:`resize()` 一起使用,用于创建给定大小的数组并对其元素进行初始化: .. tabs:: .. code-tab:: gdscript var array = [] array.resize(5) array.fill(2) print(array) # 输出 [2, 2, 2, 2, 2] .. code-tab:: csharp Godot.Collections.Array array = []; array.Resize(5); array.Fill(2); GD.Print(array); // 输出 [2, 2, 2, 2, 2] \ **注意:**\ 如果 ``value`` 是通过引用传递的 :ref:`Variant`\ (\ :ref:`Object` 派生类、\ **Array**\ 、\ :ref:`Dictionary` 等),则会用同一个 ``value`` 的引用填充该数组,即不会创建副本。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_filter: .. rst-class:: classref-method :ref:`Array` **filter**\ (\ method\: :ref:`Callable`\ ) |const| :ref:`🔗` 在数组中的每个元素上调用给定的 :ref:`Callable`\ ,并返回一个新的、经过过滤的 **Array**\ 。 该 ``method`` 接收一个数组元素作为参数,并且应返回 ``true`` 以将该元素添加到过滤后的数组中,或返回 ``false`` 以将其排除。 :: func is_even(number): return number % 2 == 0 func _ready(): print([1, 4, 5, 8].filter(is_even)) # 输出 [4, 8] # 与上面相同,但使用 lambda 函数。 print([1, 4, 5, 8].filter(func(number): return number % 2 == 0)) 另请参见 :ref:`any()`\ 、\ :ref:`all()`\ 、\ :ref:`map()` 和 :ref:`reduce()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_find: .. rst-class:: classref-method :ref:`int` **find**\ (\ what\: :ref:`Variant`, from\: :ref:`int` = 0\ ) |const| :ref:`🔗` 返回 ``what`` 在该数组中\ **第一次**\ 出现时的索引,不存在时返回 ``-1``\ 。搜索的起点可以使用 ``from`` 指定,终点为数组末尾。 \ **注意:**\ 如果你只想知道数组中是否包含 ``what``\ ,请使用 :ref:`has()`\ (C# 则为 ``Contains``\ )。在 GDScript 中,你还可以使用 ``in`` 运算符。 \ **注意:**\ 出于性能方面的考虑,搜索时会使用到 ``what`` 的 :ref:`Variant.Type`\ 。例如该方法不会认为 ``7``\ (\ :ref:`int`\ )和 ``7.0``\ (\ :ref:`float`\ )相等。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_find_custom: .. rst-class:: classref-method :ref:`int` **find_custom**\ (\ method\: :ref:`Callable`, from\: :ref:`int` = 0\ ) |const| :ref:`🔗` 返回数组中使得 ``method`` 返回 ``true`` 的\ **第一个**\ 元素的索引,若元素不存在则返回 ``-1``\ 。搜索的起始位置可以由 ``from`` 指定,搜索将继续直至数组结束。 \ ``method`` 是可调用对象,接受数组元素,返回的是 :ref:`bool`\ 。 \ **注意:**\ 如果你只想知道数组中是否包含\ *任何*\ 能够满足 ``method`` 的东西,请使用 :ref:`any()`\ 。 .. tabs:: .. code-tab:: gdscript func is_even(number): return number % 2 == 0 func _ready(): print([1, 3, 4, 7].find_custom(is_even.bind())) # 输出 2 .. rst-class:: classref-item-separator ---- .. _class_Array_method_front: .. rst-class:: classref-method :ref:`Variant` **front**\ (\ ) |const| :ref:`🔗` 返回数组的第一个元素。如果数组为空,则失败并返回 ``null``\ 。另见 :ref:`back()`\ 。 \ **注意:**\ 与 ``[]`` 运算符(\ ``array[0]``\ )不同,错误产生时不会停止项目执行。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_get: .. rst-class:: classref-method :ref:`Variant` **get**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` 返回数组中索引为 ``index`` 的元素。如果 ``index`` 越界或为负数,则该方法失败并返回 ``null``\ 。 该方法类似于 ``[]`` 运算符(但不等价)。最显著的区别就是从编辑器中运行时,该方法失败不会暂停项目运行。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_get_typed_builtin: .. rst-class:: classref-method :ref:`int` **get_typed_builtin**\ (\ ) |const| :ref:`🔗` 将类型化数组的内置 :ref:`Variant` 类型作为 :ref:`Variant.Type` 常量返回。如果该数组不是类型化的,则返回 :ref:`@GlobalScope.TYPE_NIL`\ 。另见 :ref:`is_typed()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_get_typed_class_name: .. rst-class:: classref-method :ref:`StringName` **get_typed_class_name**\ (\ ) |const| :ref:`🔗` 如果内置 :ref:`Variant` 类型为 :ref:`@GlobalScope.TYPE_OBJECT`\ ,则返回类型数组的\ **内置**\ 类名。否则,返回一个空的 :ref:`StringName`\ 。另见 :ref:`is_typed()` 和 :ref:`Object.get_class()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_get_typed_script: .. rst-class:: classref-method :ref:`Variant` **get_typed_script**\ (\ ) |const| :ref:`🔗` 返回与该类型数组关联的 :ref:`Script` 实例,如果不存在则返回 ``null``\ 。另见 :ref:`is_typed()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_has: .. rst-class:: classref-method :ref:`bool` **has**\ (\ value\: :ref:`Variant`\ ) |const| :ref:`🔗` 如果该数组包含给定的 ``value``\ ,则返回 ``true``\ 。 .. tabs:: .. code-tab:: gdscript print(["inside", 7].has("inside")) # 输出 true print(["inside", 7].has("outside")) # 输出 false print(["inside", 7].has(7)) # 输出 true print(["inside", 7].has("7")) # 输出 false .. code-tab:: csharp Godot.Collections.Array arr = ["inside", 7]; // 按照 C# 惯例,该方法重命名为 `Contains`。 GD.Print(arr.Contains("inside")); // 输出 True GD.Print(arr.Contains("outside")); // 输出 False GD.Print(arr.Contains(7)); // 输出 True GD.Print(arr.Contains("7")); // 输出 False 在 GDScript 中,这相当于 ``in`` 运算符: :: if 4 in [2, 4, 6, 8]: print("里面有 4!") # 将被输出。 \ **注意:**\ 出于性能原因,搜索会受到 ``value`` 的 :ref:`Variant.Type` 的影响。例如,对于该方法,\ ``7``\ (\ :ref:`int`\ )和 ``7.0``\ (\ :ref:`float`\ )不被视为相等。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_hash: .. rst-class:: classref-method :ref:`int` **hash**\ (\ ) |const| :ref:`🔗` 返回代表该数组及其内容的散列 32 位整数值。 \ **注意:**\ 由于哈希碰撞的缘故,哈希相同的数组\ *不*\ 保证相同。而相对的是,哈希不同的数组保证不同。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_insert: .. rst-class:: classref-method :ref:`int` **insert**\ (\ position\: :ref:`int`, value\: :ref:`Variant`\ ) :ref:`🔗` 在数组中给定索引(\ ``position``\ )处插入新元素(\ ``value``\ )。\ ``position`` 应介于 ``0`` 和数组的 :ref:`size()` 之间。如果为负数,则认为 ``position`` 为相对于数组结尾的索引。 如果成功,则返回 :ref:`@GlobalScope.OK`\ ;如果该方法失败,则返回其他 :ref:`Error` 常量之一。 \ **注意:**\ ``position`` 之后的每个元素的索引都需要向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_is_empty: .. rst-class:: classref-method :ref:`bool` **is_empty**\ (\ ) |const| :ref:`🔗` 如果数组为空(\ ``[]``\ ),则返回 ``true``\ 。另见 :ref:`size()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_is_read_only: .. rst-class:: classref-method :ref:`bool` **is_read_only**\ (\ ) |const| :ref:`🔗` 如果该数组是只读的,则返回 ``true``\ 。请参阅 :ref:`make_read_only()`\ 。 在 GDScript 中,如果数组是使用 ``const`` 关键字声明的,则该数组自动为只读。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_is_same_typed: .. rst-class:: classref-method :ref:`bool` **is_same_typed**\ (\ array\: :ref:`Array`\ ) |const| :ref:`🔗` 如果该数组的类型与给定的 ``array`` 相同,则返回 ``true``\ 。另见 :ref:`is_typed()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_is_typed: .. rst-class:: classref-method :ref:`bool` **is_typed**\ (\ ) |const| :ref:`🔗` 如果数组是类型化的,则返回 ``true``\ 。类型化数组只能包含由类型化数组构造函数定义的特定类型的元素。类型化数组的方法仍应返回通用 :ref:`Variant`\ 。 在 GDScript 中,可以使用静态类型定义类型化数组: :: var numbers: Array[float] = [0.2, 4.2, -2.0] print(numbers.is_typed()) # 输出 true .. rst-class:: classref-item-separator ---- .. _class_Array_method_make_read_only: .. rst-class:: classref-method |void| **make_read_only**\ (\ ) :ref:`🔗` 使数组只读。数组的元素无法使用其他值覆盖,顺序也无法改变。不适用于字典等嵌套的元素。 在 GDScript 中,使用 ``const`` 关键字声明的数组会自动变为只读。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_map: .. rst-class:: classref-method :ref:`Array` **map**\ (\ method\: :ref:`Callable`\ ) |const| :ref:`🔗` 为数组中的每个元素调用给定的 :ref:`Callable` 并返回一个新数组,其中填充了该 ``method`` 返回的值。 \ ``method`` 应该采用一个 :ref:`Variant` 参数(当前数组元素)并且可以返回任意 :ref:`Variant`\ 。 :: func double(number): return number * 2 func _ready(): print([1, 2, 3].map(double)) # 输出 [2, 4, 6] # 与上面相同,但使用 lambda 函数。 print([1, 2, 3].map(func(element): return element * 2)) 另请参见 :ref:`filter()`\ 、\ :ref:`reduce()`\ 、\ :ref:`any()` 和 :ref:`all()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_max: .. rst-class:: classref-method :ref:`Variant` **max**\ (\ ) |const| :ref:`🔗` 如果所有元素都可以比较,则返回数组中包含元素的最大值。否则,返回 ``null``\ 。另见 :ref:`min()`\ 。 要使用自定义比较器查找最大值,可以使用 :ref:`reduce()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_min: .. rst-class:: classref-method :ref:`Variant` **min**\ (\ ) |const| :ref:`🔗` 如果所有元素都可以比较,则返回数组中包含元素的最小值。否则,返回 ``null``\ 。另见 :ref:`max()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_pick_random: .. rst-class:: classref-method :ref:`Variant` **pick_random**\ (\ ) |const| :ref:`🔗` 从该数组中返回一个随机元素。如果数组为空,则生成一个错误并返回 ``null``\ 。 .. tabs:: .. code-tab:: gdscript # 可能输出 1、2、3.25、或 "Hi"。 print([1, 2, 3.25, "Hi"].pick_random()) .. code-tab:: csharp Godot.Collections.Array array = [1, 2, 3.25f, "Hi"]; GD.Print(array.PickRandom()); // 可能输出 1、2、3.25、或 "Hi"。 \ **注意:**\ 与引擎中的许多类似函数(例如 :ref:`@GlobalScope.randi()` 或 :ref:`shuffle()`\ )一样,该方法使用通用的全局随机种子。要从该方法获得可预测的结果,请参阅 :ref:`@GlobalScope.seed()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_pop_at: .. rst-class:: classref-method :ref:`Variant` **pop_at**\ (\ position\: :ref:`int`\ ) :ref:`🔗` 移除并返回数组中位于 ``position`` 索引处的元素。如果 ``position`` 为负数,则认为是相对于该数组末尾的值。如果数组为空,则返回 ``null``\ ;如果 ``position`` 超出范围,还会生成错误消息。 \ **注意:**\ 该方法将 ``position`` 之后每个元素的索引向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_pop_back: .. rst-class:: classref-method :ref:`Variant` **pop_back**\ (\ ) :ref:`🔗` 移除并返回数组中的末尾元素。如果数组为空,则返回 ``null``\ ,而不会生成错误。另见 :ref:`pop_front()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_pop_front: .. rst-class:: classref-method :ref:`Variant` **pop_front**\ (\ ) :ref:`🔗` 移除并返回数组的第一个元素。如果数组为空,则返回 ``null``\ ,而不会生成错误。另见 :ref:`pop_back()`\ 。 \ **注意:**\ 该方法将每个其他元素的索引向后移动,这可能会产生明显的性能成本,尤其是在较大的数组上。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_push_back: .. rst-class:: classref-method |void| **push_back**\ (\ value\: :ref:`Variant`\ ) :ref:`🔗` 在数组的末端追加一个元素。另见 :ref:`push_front()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_push_front: .. rst-class:: classref-method |void| **push_front**\ (\ value\: :ref:`Variant`\ ) :ref:`🔗` 在数组的开头添加一个元素。另见 :ref:`push_back()`\ 。 \ **注意:**\ 该方法将每个其他元素的索引向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_reduce: .. rst-class:: classref-method :ref:`Variant` **reduce**\ (\ method\: :ref:`Callable`, accum\: :ref:`Variant` = null\ ) |const| :ref:`🔗` 为数组中的每个元素调用给定的 :ref:`Callable`\ ,将结果累积在 ``accum`` 中,然后将其返回。 \ ``method`` 接受两个参数:\ ``accum`` 的当前值,以及当前的数组元素。如果 ``accum`` 为 ``null``\ (默认值),则会从第二个元素开始迭代,将第一个元素作为 ``accum`` 的初始值。 :: func sum(accum, number): return accum + number func _ready(): print([1, 2, 3].reduce(sum, 0)) # 输出 6 print([1, 2, 3].reduce(sum, 10)) # 输出 16 # 与上面相同,但是使用 lambda 函数。 print([1, 2, 3].reduce(func(accum, number): return accum + number, 10)) 如果 :ref:`max()` 无法满足需求,也可以使用该方法来实现自定义比较器: :: func _ready(): var arr = [Vector2i(5, 0), Vector2i(3, 4), Vector2i(1, 2)] var longest_vec = arr.reduce(func(max, vec): return vec if is_length_greater(vec, max) else max) print(longest_vec) # 输出 (3, 4) func is_length_greater(a, b): return a.length() > b.length() 该方法还可以用来计算数组中满足特定条件元素的数量,与 :ref:`count()` 类似: :: func is_even(number): return number % 2 == 0 func _ready(): var arr = [1, 2, 3, 4, 5] # 当前元素为偶数则增加计数器,否则保持计数不变。 var even_count = arr.reduce(func(count, next): return count + 1 if is_even(next) else count, 0) print(even_count) # 输出 2 另见 :ref:`map()`\ 、\ :ref:`filter()`\ 、\ :ref:`any()`\ 、\ :ref:`all()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_remove_at: .. rst-class:: classref-method |void| **remove_at**\ (\ position\: :ref:`int`\ ) :ref:`🔗` 从数组中移除指定索引(\ ``position``\ )处的元素。如果索引超出范围,则该方法失败。如果为负数,则认为 ``position`` 为相对于数组结尾的索引。 如果需要返回被移除的元素,请使用 :ref:`pop_at()`\ 。要按值移除元素,请改用 :ref:`erase()`\ 。 \ **注意:**\ 该方法将 ``position`` 之后每个元素的索引向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。 \ **注意:**\ ``position`` 不能为负数。要移除相对于数组末尾的元素,请使用 ``arr.remove_at(arr.size() - (i + 1))``\ 。要从数组中移除最后一个元素,请使用 ``arr.resize(arr.size() - 1)``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_resize: .. rst-class:: classref-method :ref:`int` **resize**\ (\ size\: :ref:`int`\ ) :ref:`🔗` 将数组的元素数设置为 ``size``\ 。如果 ``size`` 小于数组的当前大小,则移除末尾的元素。如果 ``size`` 大于数组的当前大小,则添加新的默认元素(通常为 ``null``\ ),具体取决于数组的类型。 如果成功则返回 :ref:`@GlobalScope.OK`\ ,如果该方法失败则返回其他 :ref:`Error` 常量之一:数组只读时为 :ref:`@GlobalScope.ERR_LOCKED`\ ,大小为负数时为 :ref:`@GlobalScope.ERR_INVALID_PARAMETER`\ ,分配失败时为 :ref:`@GlobalScope.ERR_OUT_OF_MEMORY`\ 。请使用 :ref:`size()` 查看数组调整后的实际大小。 \ **注意:**\ 调用该方法一次并分配新值,要比为每个新元素调用 :ref:`append()` 更快。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_reverse: .. rst-class:: classref-method |void| **reverse**\ (\ ) :ref:`🔗` 反转数组中所有元素的顺序。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_rfind: .. rst-class:: classref-method :ref:`int` **rfind**\ (\ what\: :ref:`Variant`, from\: :ref:`int` = -1\ ) |const| :ref:`🔗` 返回该数组中 ``what`` **最后一次**\ 出现时的索引,不存在时则为 ``-1``\ 。搜索的起点可以用 ``from`` 指定,终点为该数组的开头。该方法与 :ref:`find()` 相对。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_rfind_custom: .. rst-class:: classref-method :ref:`int` **rfind_custom**\ (\ method\: :ref:`Callable`, from\: :ref:`int` = -1\ ) |const| :ref:`🔗` 返回数组中能够让 ``method`` 返回 ``true`` 的\ **最后一个**\ 元素的索引,不存在时返回 ``-1``\ 。搜索的起点可以用 ``from`` 指定,搜索的终点为数组的开头。该方法与 :ref:`find_custom()` 相对。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_set: .. rst-class:: classref-method |void| **set**\ (\ index\: :ref:`int`, value\: :ref:`Variant`\ ) :ref:`🔗` 将索引为 ``index`` 的元素的值设置为 ``value``\ 。数组的大小不会发生改变,改变的只有数组中现有索引所对应的值。与使用 ``[]`` 运算符相同(\ ``array[index] = value``\ )。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_shuffle: .. rst-class:: classref-method |void| **shuffle**\ (\ ) :ref:`🔗` 随机打乱数组中所有元素的顺序。 \ **注意:**\ 与引擎中很多类似的函数一样(例如 :ref:`@GlobalScope.randi()` 和 :ref:`pick_random()`\ ),该方法使用的是通用的全局随机数种子。如何获取可预知的结果见 :ref:`@GlobalScope.seed()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_size: .. rst-class:: classref-method :ref:`int` **size**\ (\ ) |const| :ref:`🔗` 返回该数组中元素的数量。空数组(\ ``[]``\ )始终返回 ``0``\ 。另见 :ref:`is_empty()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_slice: .. rst-class:: classref-method :ref:`Array` **slice**\ (\ begin\: :ref:`int`, end\: :ref:`int` = 2147483647, step\: :ref:`int` = 1, deep\: :ref:`bool` = false\ ) |const| :ref:`🔗` 返回一个新的 **Array**\ ,其中包含该数组的元素,从索引 ``begin``\ (含)到 ``end``\ (不含),每个 ``step`` 个元素。 如果 ``begin`` 或 ``end`` 为负数,则它们的值相对于数组的末尾。 如果 ``step`` 为负数,则该方法反向遍历数组,返回按反向顺序排列的切片数组。要使其起作用,\ ``begin`` 必须大于 ``end``\ 。 如果 ``deep`` 为 ``true``\ ,则切片数组中所有嵌套的 **Array** 和 :ref:`Dictionary` 元素都将从原始元素中递归复制。另见 :ref:`duplicate()`\ 。 :: var letters = ["A", "B", "C", "D", "E", "F"] print(letters.slice(0, 2)) # 输出 ["A", "B"] print(letters.slice(2, -2)) # 输出 ["C", "D"] print(letters.slice(-2, 6)) # 输出 ["E", "F"] print(letters.slice(0, 6, 2)) # 输出 ["A", "C", "E"] print(letters.slice(4, 1, -1)) # 输出 ["E", "D", "C"] .. rst-class:: classref-item-separator ---- .. _class_Array_method_sort: .. rst-class:: classref-method |void| **sort**\ (\ ) :ref:`🔗` 按升序对数组进行排序。最终顺序取决于元素之间的“小于”(\ ``>``\ )比较。 .. tabs:: .. code-tab:: gdscript var numbers = [10, 5, 2.5, 8] numbers.sort() print(numbers) # 输出 [2.5, 5, 8, 10] .. code-tab:: csharp Godot.Collections.Array numbers = [10, 5, 2.5, 8]; numbers.Sort(); GD.Print(numbers); // 输出 [2.5, 5, 8, 10] \ **注意:**\ 使用的排序算法并不\ `稳定 `__\ 。这意味着等效元素(例如 ``2`` 和 ``2.0``\ )在调用 :ref:`sort()` 时可能会改变其顺序。 .. rst-class:: classref-item-separator ---- .. _class_Array_method_sort_custom: .. rst-class:: classref-method |void| **sort_custom**\ (\ func\: :ref:`Callable`\ ) :ref:`🔗` 使用自定义的 :ref:`Callable` 对数组进行排序。 \ ``func`` 可根据需要多次调用,接收两个数组元素作为参数。如果第一个元素应移到第二个元素的\ *前面*\ ,则该函数应返回 ``true``\ ,否则应返回 ``false``\ 。 :: func sort_ascending(a, b): if a[1] < b[1]: return true return false func _ready(): var my_items = [["Tomato", 5], ["Apple", 9], ["Rice", 4]] my_items.sort_custom(sort_ascending) print(my_items) # 输出 [["Rice", 4], ["Tomato", 5], ["Apple", 9]] # 使用 lambda 函数按降序排序。 my_items.sort_custom(func(a, b): return a[0] > b[0]) print(my_items) # 输出 [["Apple", 9], ["Tomato", 5], ["Rice", 4]] 可能还需要使用该方法按自然顺序对字符串进行排序,使用 :ref:`String.naturalnocasecmp_to()`\ ,如下例所示: :: var files = ["newfile1", "newfile2", "newfile10", "newfile11"] files.sort_custom(func(a, b): return a.naturalnocasecmp_to(b) < 0) print(files) # 输出 ["newfile1", "newfile2", "newfile10", "newfile11"] \ **注意:**\ 在 C# 中,不支持该方法。 \ **注意:**\ 使用的排序算法并不是\ `稳定的 `__\ 。这意味着在调用该方法时,被视为相等的值的顺序可能会发生变化。 \ **注意:**\ 不应随机化 ``func`` 的返回值,因为堆排序算法需要一致的结果。随机化返回值将导致意外行为。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 运算符说明 ---------- .. _class_Array_operator_neq_Array: .. rst-class:: classref-operator :ref:`bool` **operator !=**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 如果该数组的大小或其元素与 ``right`` 不同,则返回 ``true``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_sum_Array: .. rst-class:: classref-operator :ref:`Array` **operator +**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 将 ``right`` 数组追加到左操作数,会创建一个新的 **Array**\ 。这也称为数组拼接。 .. tabs:: .. code-tab:: gdscript var array1 = ["One", 2] var array2 = [3, "Four"] print(array1 + array2) # 输出 ["One", 2, 3, "Four"] .. code-tab:: csharp // 请注意,C# 的原生数组类型无法进行拼接。 Godot.Collections.Array array1 = ["One", 2]; Godot.Collections.Array array2 = [3, "Four"]; GD.Print(array1 + array2); // 输出 ["One", 2, 3, "Four"] \ **注意:**\ 对于已有数组,\ :ref:`append_array()` 比使用 ``+=`` 运算符的拼接和赋值效率高得多。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_lt_Array: .. rst-class:: classref-operator :ref:`bool` **operator <**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素小于 ``right`` 的元素,则返回 ``true``\ ;如果该元素大于 ``right`` 的元素,则返回 ``false``\ 。否则,继续下一对。 当所有搜索到的元素都相等时,如果该数组的大小小于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_lte_Array: .. rst-class:: classref-operator :ref:`bool` **operator <=**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素小于 ``right`` 的元素,则返回 ``true``\ ;如果该元素较大则返回 ``false``\ 。否则,继续下一对。 当所有搜索到的元素都相等时,如果该数组的大小小于或等于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_eq_Array: .. rst-class:: classref-operator :ref:`bool` **operator ==**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 将左操作数 **Array** 与 ``right`` **Array** 进行比较。如果数组的大小和内容相等,则返回 ``true``\ ,否则返回 ``false``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_gt_Array: .. rst-class:: classref-operator :ref:`bool` **operator >**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素大于 ``right`` 的元素,则返回 ``true``\ ;如果该元素较小则返回 ``false``\ 。否则,继续下一对。 当所有搜索到的元素都相等时,如果该数组的大小大于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_gte_Array: .. rst-class:: classref-operator :ref:`bool` **operator >=**\ (\ right\: :ref:`Array`\ ) :ref:`🔗` 按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素大于 ``right`` 的元素,则返回 ``true``\ ,如果该元素较小则返回 ``false``\ 。否则,继续下一对。 当所有搜索到的元素都相等时,如果该数组的大小大于或等于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。 .. rst-class:: classref-item-separator ---- .. _class_Array_operator_idx_int: .. rst-class:: classref-operator :ref:`Variant` **operator []**\ (\ index\: :ref:`int`\ ) :ref:`🔗` 返回指定 ``index`` 处的 :ref:`Variant` 元素。数组从索引 0 开始。如果 ``index`` 大于或等于 ``0``\ ,则从数组开头开始获取元素。如果 ``index`` 为负值,则从末尾开始获取元素。越界访问数组将导致运行时错误,从编辑器中运行时会暂停项目执行。 .. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (本方法无副作用,不会修改该实例的任何成员变量。)` .. |vararg| replace:: :abbr:`vararg (本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。)` .. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)` .. |static| replace:: :abbr:`static (调用本方法无需实例,可直接使用类名进行调用。)` .. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效运算符。)` .. |bitfield| replace:: :abbr:`BitField (这个值是由下列位标志构成位掩码的整数。)` .. |void| replace:: :abbr:`void (无返回值。)`