mirror of
https://github.com/godotengine/godot-docs-l10n.git
synced 2025-12-31 09:49:22 +03:00
This reverts commit 57781dc0bc.
The rename, although in line with what we did in the engine, caused problems with RTD we need to fix (or push for a fix) first.
1569 lines
85 KiB
ReStructuredText
1569 lines
85 KiB
ReStructuredText
:github_url: hide
|
||
|
||
.. _class_Array:
|
||
|
||
Array
|
||
=====
|
||
|
||
一种内置数据结构,包含一系列元素。
|
||
|
||
.. rst-class:: classref-introduction-group
|
||
|
||
描述
|
||
----
|
||
|
||
数组数据类型,默认可以包含任意 :ref:`Variant<class_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<int> typedArray = [1, 2, 3];
|
||
|
||
|
||
|
||
\ **注意:**\ 数组始终按\ **引用**\ 传递。如果要获取数组的副本,让改动独立于原始数组,请使用 :ref:`duplicate()<class_Array_method_duplicate>`\ 。
|
||
|
||
\ **注意:**\ **不支持**\ 在遍历数组元素时擦除元素,这样做可能造成预料之外的行为。
|
||
|
||
\ **紧缩数组、类型数组、无类型数组:**\ 紧缩数组在遍历和修改时通常比同类型的类型数组要快(例如将 :ref:`PackedInt64Array<class_PackedInt64Array>` 和 ``Array[int]`` 相比)。紧缩数组占据的内存也相对较少。但紧缩数组的缺点是不够灵活,因为没有提供 :ref:`map()<class_Array_method_map>` 之类的便捷方法。相应地,类型数组在遍历和修改时要比无类型数组要快。
|
||
|
||
.. note::
|
||
|
||
通过 C# 使用该 API 时会有显著不同,详见 :ref:`doc_c_sharp_differences`\ 。
|
||
|
||
.. rst-class:: classref-reftable-group
|
||
|
||
构造函数
|
||
--------
|
||
|
||
.. table::
|
||
:widths: auto
|
||
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ base\: :ref:`Array<class_Array>`, type\: :ref:`int<class_int>`, class_name\: :ref:`StringName<class_StringName>`, script\: :ref:`Variant<class_Variant>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`Array<class_Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedColorArray<class_PackedColorArray>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedFloat32Array<class_PackedFloat32Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedFloat64Array<class_PackedFloat64Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedInt32Array<class_PackedInt32Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedInt64Array<class_PackedInt64Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedStringArray<class_PackedStringArray>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedVector2Array<class_PackedVector2Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedVector3Array<class_PackedVector3Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedVector4Array<class_PackedVector4Array>`\ ) |
|
||
+---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
|
||
.. rst-class:: classref-reftable-group
|
||
|
||
方法
|
||
----
|
||
|
||
.. table::
|
||
:widths: auto
|
||
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`all<class_Array_method_all>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`any<class_Array_method_any>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`append<class_Array_method_append>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`append_array<class_Array_method_append_array>`\ (\ array\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`assign<class_Array_method_assign>`\ (\ array\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`back<class_Array_method_back>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`bsearch<class_Array_method_bsearch>`\ (\ value\: :ref:`Variant<class_Variant>`, before\: :ref:`bool<class_bool>` = true\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_method_bsearch_custom>`\ (\ value\: :ref:`Variant<class_Variant>`, func\: :ref:`Callable<class_Callable>`, before\: :ref:`bool<class_bool>` = true\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`clear<class_Array_method_clear>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`count<class_Array_method_count>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`duplicate<class_Array_method_duplicate>`\ (\ deep\: :ref:`bool<class_bool>` = false\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`duplicate_deep<class_Array_method_duplicate_deep>`\ (\ deep_subresources_mode\: :ref:`int<class_int>` = 1\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`erase<class_Array_method_erase>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`fill<class_Array_method_fill>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`filter<class_Array_method_filter>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`find<class_Array_method_find>`\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = 0\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`find_custom<class_Array_method_find_custom>`\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = 0\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`front<class_Array_method_front>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`get<class_Array_method_get>`\ (\ index\: :ref:`int<class_int>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`get_typed_builtin<class_Array_method_get_typed_builtin>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`StringName<class_StringName>` | :ref:`get_typed_class_name<class_Array_method_get_typed_class_name>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`get_typed_script<class_Array_method_get_typed_script>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`has<class_Array_method_has>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`hash<class_Array_method_hash>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`insert<class_Array_method_insert>`\ (\ position\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`is_empty<class_Array_method_is_empty>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`is_read_only<class_Array_method_is_read_only>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`is_same_typed<class_Array_method_is_same_typed>`\ (\ array\: :ref:`Array<class_Array>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`is_typed<class_Array_method_is_typed>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`make_read_only<class_Array_method_make_read_only>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`map<class_Array_method_map>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`max<class_Array_method_max>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`min<class_Array_method_min>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`pick_random<class_Array_method_pick_random>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`pop_at<class_Array_method_pop_at>`\ (\ position\: :ref:`int<class_int>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`pop_back<class_Array_method_pop_back>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`pop_front<class_Array_method_pop_front>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`push_back<class_Array_method_push_back>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`push_front<class_Array_method_push_front>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`reduce<class_Array_method_reduce>`\ (\ method\: :ref:`Callable<class_Callable>`, accum\: :ref:`Variant<class_Variant>` = null\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`remove_at<class_Array_method_remove_at>`\ (\ position\: :ref:`int<class_int>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`resize<class_Array_method_resize>`\ (\ size\: :ref:`int<class_int>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`reverse<class_Array_method_reverse>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`rfind<class_Array_method_rfind>`\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = -1\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`rfind_custom<class_Array_method_rfind_custom>`\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = -1\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`set<class_Array_method_set>`\ (\ index\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`shuffle<class_Array_method_shuffle>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`int<class_int>` | :ref:`size<class_Array_method_size>`\ (\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`slice<class_Array_method_slice>`\ (\ begin\: :ref:`int<class_int>`, end\: :ref:`int<class_int>` = 2147483647, step\: :ref:`int<class_int>` = 1, deep\: :ref:`bool<class_bool>` = false\ ) |const| |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`sort<class_Array_method_sort>`\ (\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
| |void| | :ref:`sort_custom<class_Array_method_sort_custom>`\ (\ func\: :ref:`Callable<class_Callable>`\ ) |
|
||
+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
||
|
||
.. rst-class:: classref-reftable-group
|
||
|
||
运算符
|
||
------
|
||
|
||
.. table::
|
||
:widths: auto
|
||
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`operator !=<class_Array_operator_neq_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`Array<class_Array>` | :ref:`operator +<class_Array_operator_sum_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`operator \<<class_Array_operator_lt_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`operator \<=<class_Array_operator_lte_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`operator ==<class_Array_operator_eq_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`operator ><class_Array_operator_gt_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`bool<class_bool>` | :ref:`operator >=<class_Array_operator_gte_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
| :ref:`Variant<class_Variant>` | :ref:`operator []<class_Array_operator_idx_int>`\ (\ index\: :ref:`int<class_int>`\ ) |
|
||
+-------------------------------+----------------------------------------------------------------------------------------------+
|
||
|
||
.. rst-class:: classref-section-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-descriptions-group
|
||
|
||
构造函数说明
|
||
------------
|
||
|
||
.. _class_Array_constructor_Array:
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ ) :ref:`🔗<class_Array_constructor_Array>`
|
||
|
||
构造空的 **Array**\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ base\: :ref:`Array<class_Array>`, type\: :ref:`int<class_int>`, class_name\: :ref:`StringName<class_StringName>`, script\: :ref:`Variant<class_Variant>`\ )
|
||
|
||
根据 ``base`` 数组创建类型化的数组。类型化的数组只能包含给定类型的元素,或者从给定类继承的元素,构造函数的参数如下所述:
|
||
|
||
- ``type`` 是内置 :ref:`Variant<class_Variant>` 类型,是一个 :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` 常量。
|
||
|
||
- ``class_name`` 是内置类名(见 :ref:`Object.get_class()<class_Object_method_get_class>`\ )。
|
||
|
||
- ``script`` 是关联的脚本。它必须是 :ref:`Script<class_Script>` 实例或 ``null``\ 。
|
||
|
||
如果 ``type`` 不是 :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`\ ,则 ``class_name`` 必须为空的 :ref:`StringName<class_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<class_Array>` **Array**\ (\ from\: :ref:`Array<class_Array>`\ )
|
||
|
||
返回与 ``from`` 相同的数组。如果你需要一个数组的副本,请使用 :ref:`duplicate()<class_Array_method_duplicate>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedByteArray<class_PackedByteArray>`\ )
|
||
|
||
从 :ref:`PackedByteArray<class_PackedByteArray>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedColorArray<class_PackedColorArray>`\ )
|
||
|
||
从 :ref:`PackedColorArray<class_PackedColorArray>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedFloat32Array<class_PackedFloat32Array>`\ )
|
||
|
||
从 :ref:`PackedFloat32Array<class_PackedFloat32Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedFloat64Array<class_PackedFloat64Array>`\ )
|
||
|
||
从 :ref:`PackedFloat64Array<class_PackedFloat64Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedInt32Array<class_PackedInt32Array>`\ )
|
||
|
||
从 :ref:`PackedInt32Array<class_PackedInt32Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedInt64Array<class_PackedInt64Array>`\ )
|
||
|
||
从 :ref:`PackedInt64Array<class_PackedInt64Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedStringArray<class_PackedStringArray>`\ )
|
||
|
||
从 :ref:`PackedStringArray<class_PackedStringArray>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedVector2Array<class_PackedVector2Array>`\ )
|
||
|
||
从 :ref:`PackedVector2Array<class_PackedVector2Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedVector3Array<class_PackedVector3Array>`\ )
|
||
|
||
从 :ref:`PackedVector3Array<class_PackedVector3Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-constructor
|
||
|
||
:ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedVector4Array<class_PackedVector4Array>`\ )
|
||
|
||
从 :ref:`PackedVector4Array<class_PackedVector4Array>` 构造一个数组。
|
||
|
||
.. rst-class:: classref-section-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-descriptions-group
|
||
|
||
方法说明
|
||
--------
|
||
|
||
.. _class_Array_method_all:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **all**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`🔗<class_Array_method_all>`
|
||
|
||
对数组中的每个元素调用给定的 :ref:`Callable<class_Callable>`\ ,如果 :ref:`Callable<class_Callable>` 为数组中的 *所有* 元素返回 ``true``\ ,则返回 ``true``\ 。如果 :ref:`Callable<class_Callable>` 为一个或多个数组元素返回 ``false``\ ,则此方法返回 ``false``\ 。
|
||
|
||
该 ``method`` 应采用一个 :ref:`Variant<class_Variant>` 参数(当前数组元素)并返回一个 :ref:`bool<class_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()<class_Array_method_any>`\ 、\ :ref:`filter()<class_Array_method_filter>`\ 、\ :ref:`map()<class_Array_method_map>` 和 :ref:`reduce()<class_Array_method_reduce>`\ 。
|
||
|
||
\ **注意:**\ 与依赖 :ref:`filter()<class_Array_method_filter>` 返回的数组大小不同,此方法会尽可能早地返回以提高性能(尤其是对于大型数组)。
|
||
|
||
\ **注意:**\ 对于空数组,此方法 `总是 <https://en.wikipedia.org/wiki/Vacuous_truth>`__ 返回 ``true``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_any:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **any**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`🔗<class_Array_method_any>`
|
||
|
||
对数组中的每个元素调用给定的 :ref:`Callable<class_Callable>`\ ,如果 :ref:`Callable<class_Callable>` 为数组中的\ * 一个或多个*\ 元素返回 ``true``\ ,则返回 ``true``\ 。如果 :ref:`Callable<class_Callable>` 为数组中的所有元素返回 ``false``\ ,则该方法返回 ``false``\ 。
|
||
|
||
该 ``method`` 应接受一个 :ref:`Variant<class_Variant>` 参数(当前数组元素)并返回一个 :ref:`bool<class_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()<class_Array_method_all>`\ 、\ :ref:`filter()<class_Array_method_filter>`\ 、\ :ref:`map()<class_Array_method_map>` 和 :ref:`reduce()<class_Array_method_reduce>`\ 。
|
||
|
||
\ **注意:**\ 与依赖 :ref:`filter()<class_Array_method_filter>` 返回的数组大小不同,此方法会尽可能早地返回以提高性能(尤其是对于大型数组)。
|
||
|
||
\ **注意:**\ 对于一个空数组,这个方法总是返回 ``false``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_append:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **append**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_append>`
|
||
|
||
将 ``value`` 追加到数组末尾(\ :ref:`push_back()<class_Array_method_push_back>` 的别名)。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_append_array:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **append_array**\ (\ array\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_method_append_array>`
|
||
|
||
在该数组的末尾追加其他 ``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<class_Array>`\ ) :ref:`🔗<class_Array_method_assign>`
|
||
|
||
将另一个 ``array`` 的元素赋值到该数组中。调整数组大小以匹配 ``array``\ 。如果数组是有类型的,则执行类型转换。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_back:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **back**\ (\ ) |const| :ref:`🔗<class_Array_method_back>`
|
||
|
||
返回数组的最后一个元素。如果数组为空,则失败并返回 ``null``\ 。另见 :ref:`front()<class_Array_method_front>`\ 。
|
||
|
||
\ **注意:**\ 与 ``[]`` 运算符(\ ``array[-1]``\ )不同,错误生成时不会停止项目执行。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_bsearch:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **bsearch**\ (\ value\: :ref:`Variant<class_Variant>`, before\: :ref:`bool<class_bool>` = true\ ) |const| :ref:`🔗<class_Array_method_bsearch>`
|
||
|
||
返回已排序数组中 ``value`` 的索引。如果找不到,则返回应被插入 ``value`` 的位置以保持数组被排序。使用的算法是\ `二分查找算法 <https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95>`__\ 。
|
||
|
||
如果 ``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()<class_Array_method_bsearch>` 将导致意外行为。调用该方法之前,请使用 :ref:`sort()<class_Array_method_sort>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_bsearch_custom:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **bsearch_custom**\ (\ value\: :ref:`Variant<class_Variant>`, func\: :ref:`Callable<class_Callable>`, before\: :ref:`bool<class_bool>` = true\ ) |const| :ref:`🔗<class_Array_method_bsearch_custom>`
|
||
|
||
返回已排序数组中 ``value`` 的索引。如果找不到,则返回 ``value`` 应插入的位置,以保持数组已排序(使用 ``func`` 进行比较)。使用的算法是\ `二分查找算法 <https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95>`__\ 。
|
||
|
||
与 :ref:`sort_custom()<class_Array_method_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()<class_Array_method_bsearch_custom>` 将导致意外行为。在调用该方法之前,请将 :ref:`sort_custom()<class_Array_method_sort_custom>` 与 ``func`` 结合使用。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_clear:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **clear**\ (\ ) :ref:`🔗<class_Array_method_clear>`
|
||
|
||
从该数组中移除所有元素。相当于调用 :ref:`resize()<class_Array_method_resize>` 时指定大小为 ``0``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_count:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **count**\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| :ref:`🔗<class_Array_method_count>`
|
||
|
||
返回数组中某个元素出现的次数。
|
||
|
||
要计算某个数组中有多少元素满足某个条件,参见\ :ref:`reduce()<class_Array_method_reduce>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_duplicate:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Array<class_Array>` **duplicate**\ (\ deep\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`🔗<class_Array_method_duplicate>`
|
||
|
||
返回数组的新副本。
|
||
|
||
默认情况下返回的是\ **浅拷贝**\ :嵌套的 **Array**\ 、\ :ref:`Dictionary<class_Dictionary>` 和 :ref:`Resource<class_Resource>` 元素与原数组共享。对这些元素的修改会影响另一个数组。
|
||
|
||
如果 ``deep`` 为 ``true`` 则会返回\ **深拷贝**\ :嵌套的数组和字典也会进行(递归的)复制。不过 :ref:`Resource<class_Resource>` 仍然是和原数组共享的。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_duplicate_deep:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Array<class_Array>` **duplicate_deep**\ (\ deep_subresources_mode\: :ref:`int<class_int>` = 1\ ) |const| :ref:`🔗<class_Array_method_duplicate_deep>`
|
||
|
||
深度复制该数组,类似 :ref:`duplicate()<class_Array_method_duplicate>`\ ``(true)``\ ,但能够额外控制子资源的处理方式。
|
||
|
||
\ ``deep_subresources_mode`` 必须是 :ref:`DeepDuplicateMode<enum_Resource_DeepDuplicateMode>` 中的一个值。默认只会(递归)复制内部资源。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_erase:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **erase**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_erase>`
|
||
|
||
查找并从数组中移除 ``value`` 的第一个匹配值。如果数组中不存在 ``value``\ ,则什么也不会发生。要通过索引移除元素,请改用 :ref:`remove_at()<class_Array_method_remove_at>`\ 。
|
||
|
||
\ **注意:**\ 该方法将移除的 ``value`` 后每个元素的索引移回一位,这可能会产生明显的性能成本,尤其是在较大的数组上。
|
||
|
||
\ **注意:**\ 在迭代数组时移除元素\ **不**\ 受支持,并且将导致不可预测的行为。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_fill:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **fill**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_fill>`
|
||
|
||
将该数组中的所有元素都设置为给定的 ``value``\ 。
|
||
|
||
该方法通常与 :ref:`resize()<class_Array_method_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<class_Variant>`\ (\ :ref:`Object<class_Object>` 派生类、\ **Array**\ 、\ :ref:`Dictionary<class_Dictionary>` 等),则会用同一个 ``value`` 的引用填充该数组,即不会创建副本。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_filter:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Array<class_Array>` **filter**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`🔗<class_Array_method_filter>`
|
||
|
||
在数组中的每个元素上调用给定的 :ref:`Callable<class_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()<class_Array_method_any>`\ 、\ :ref:`all()<class_Array_method_all>`\ 、\ :ref:`map()<class_Array_method_map>` 和 :ref:`reduce()<class_Array_method_reduce>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_find:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **find**\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = 0\ ) |const| :ref:`🔗<class_Array_method_find>`
|
||
|
||
返回 ``what`` 在该数组中\ **第一次**\ 出现时的索引,不存在时返回 ``-1``\ 。搜索的起点可以使用 ``from`` 指定,终点为数组末尾。
|
||
|
||
\ **注意:**\ 如果你只想知道数组中是否包含 ``what``\ ,请使用 :ref:`has()<class_Array_method_has>`\ (C# 则为 ``Contains``\ )。在 GDScript 中,你还可以使用 ``in`` 运算符。
|
||
|
||
\ **注意:**\ 出于性能方面的考虑,搜索时会使用到 ``what`` 的 :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>`\ 。例如该方法不会认为 ``7``\ (\ :ref:`int<class_int>`\ )和 ``7.0``\ (\ :ref:`float<class_float>`\ )相等。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_find_custom:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **find_custom**\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = 0\ ) |const| :ref:`🔗<class_Array_method_find_custom>`
|
||
|
||
返回数组中使得 ``method`` 返回 ``true`` 的\ **第一个**\ 元素的索引,若元素不存在则返回 ``-1``\ 。搜索的起始位置可以由 ``from`` 指定,搜索将继续直至数组结束。
|
||
|
||
\ ``method`` 是可调用对象,接受数组元素,返回的是 :ref:`bool<class_bool>`\ 。
|
||
|
||
\ **注意:**\ 如果你只想知道数组中是否包含\ *任何*\ 能够满足 ``method`` 的东西,请使用 :ref:`any()<class_Array_method_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<class_Variant>` **front**\ (\ ) |const| :ref:`🔗<class_Array_method_front>`
|
||
|
||
返回数组的第一个元素。如果数组为空,则失败并返回 ``null``\ 。另见 :ref:`back()<class_Array_method_back>`\ 。
|
||
|
||
\ **注意:**\ 与 ``[]`` 运算符(\ ``array[0]``\ )不同,错误产生时不会停止项目执行。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_get:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **get**\ (\ index\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_Array_method_get>`
|
||
|
||
返回数组中索引为 ``index`` 的元素。如果 ``index`` 越界或为负数,则该方法失败并返回 ``null``\ 。
|
||
|
||
该方法类似于 ``[]`` 运算符(但不等价)。最显著的区别就是从编辑器中运行时,该方法失败不会暂停项目运行。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_get_typed_builtin:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **get_typed_builtin**\ (\ ) |const| :ref:`🔗<class_Array_method_get_typed_builtin>`
|
||
|
||
将类型化数组的内置 :ref:`Variant<class_Variant>` 类型作为 :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` 常量返回。如果该数组不是类型化的,则返回 :ref:`@GlobalScope.TYPE_NIL<class_@GlobalScope_constant_TYPE_NIL>`\ 。另见 :ref:`is_typed()<class_Array_method_is_typed>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_get_typed_class_name:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`StringName<class_StringName>` **get_typed_class_name**\ (\ ) |const| :ref:`🔗<class_Array_method_get_typed_class_name>`
|
||
|
||
如果内置 :ref:`Variant<class_Variant>` 类型为 :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`\ ,则返回类型数组的\ **内置**\ 类名。否则,返回一个空的 :ref:`StringName<class_StringName>`\ 。另见 :ref:`is_typed()<class_Array_method_is_typed>` 和 :ref:`Object.get_class()<class_Object_method_get_class>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_get_typed_script:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **get_typed_script**\ (\ ) |const| :ref:`🔗<class_Array_method_get_typed_script>`
|
||
|
||
返回与该类型数组关联的 :ref:`Script<class_Script>` 实例,如果不存在则返回 ``null``\ 。另见 :ref:`is_typed()<class_Array_method_is_typed>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_has:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **has**\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| :ref:`🔗<class_Array_method_has>`
|
||
|
||
如果该数组包含给定的 ``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<enum_@GlobalScope_Variant.Type>` 的影响。例如,对于该方法,\ ``7``\ (\ :ref:`int<class_int>`\ )和 ``7.0``\ (\ :ref:`float<class_float>`\ )不被视为相等。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_hash:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **hash**\ (\ ) |const| :ref:`🔗<class_Array_method_hash>`
|
||
|
||
返回代表该数组及其内容的散列 32 位整数值。
|
||
|
||
\ **注意:**\ 由于哈希碰撞的缘故,哈希相同的数组\ *不*\ 保证相同。而相对的是,哈希不同的数组保证不同。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_insert:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **insert**\ (\ position\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_insert>`
|
||
|
||
在数组中给定索引(\ ``position``\ )处插入新元素(\ ``value``\ )。\ ``position`` 应介于 ``0`` 和数组的 :ref:`size()<class_Array_method_size>` 之间。如果为负数,则认为 ``position`` 为相对于数组结尾的索引。
|
||
|
||
如果成功,则返回 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ ;如果该方法失败,则返回其他 :ref:`Error<enum_@GlobalScope_Error>` 常量之一。
|
||
|
||
\ **注意:**\ ``position`` 之后的每个元素的索引都需要向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_is_empty:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **is_empty**\ (\ ) |const| :ref:`🔗<class_Array_method_is_empty>`
|
||
|
||
如果数组为空(\ ``[]``\ ),则返回 ``true``\ 。另见 :ref:`size()<class_Array_method_size>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_is_read_only:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **is_read_only**\ (\ ) |const| :ref:`🔗<class_Array_method_is_read_only>`
|
||
|
||
如果该数组是只读的,则返回 ``true``\ 。请参阅 :ref:`make_read_only()<class_Array_method_make_read_only>`\ 。
|
||
|
||
在 GDScript 中,如果数组是使用 ``const`` 关键字声明的,则该数组自动为只读。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_is_same_typed:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **is_same_typed**\ (\ array\: :ref:`Array<class_Array>`\ ) |const| :ref:`🔗<class_Array_method_is_same_typed>`
|
||
|
||
如果该数组的类型与给定的 ``array`` 相同,则返回 ``true``\ 。另见 :ref:`is_typed()<class_Array_method_is_typed>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_is_typed:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`bool<class_bool>` **is_typed**\ (\ ) |const| :ref:`🔗<class_Array_method_is_typed>`
|
||
|
||
如果数组是类型化的,则返回 ``true``\ 。类型化数组只能包含由类型化数组构造函数定义的特定类型的元素。类型化数组的方法仍应返回通用 :ref:`Variant<class_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:`🔗<class_Array_method_make_read_only>`
|
||
|
||
使数组只读。数组的元素无法使用其他值覆盖,顺序也无法改变。不适用于字典等嵌套的元素。
|
||
|
||
在 GDScript 中,使用 ``const`` 关键字声明的数组会自动变为只读。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_map:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Array<class_Array>` **map**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`🔗<class_Array_method_map>`
|
||
|
||
为数组中的每个元素调用给定的 :ref:`Callable<class_Callable>` 并返回一个新数组,其中填充了该 ``method`` 返回的值。
|
||
|
||
\ ``method`` 应该采用一个 :ref:`Variant<class_Variant>` 参数(当前数组元素)并且可以返回任意 :ref:`Variant<class_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()<class_Array_method_filter>`\ 、\ :ref:`reduce()<class_Array_method_reduce>`\ 、\ :ref:`any()<class_Array_method_any>` 和 :ref:`all()<class_Array_method_all>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_max:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **max**\ (\ ) |const| :ref:`🔗<class_Array_method_max>`
|
||
|
||
如果所有元素都可以比较,则返回数组中包含元素的最大值。否则,返回 ``null``\ 。另见 :ref:`min()<class_Array_method_min>`\ 。
|
||
|
||
要使用自定义比较器查找最大值,可以使用 :ref:`reduce()<class_Array_method_reduce>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_min:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **min**\ (\ ) |const| :ref:`🔗<class_Array_method_min>`
|
||
|
||
如果所有元素都可以比较,则返回数组中包含元素的最小值。否则,返回 ``null``\ 。另见 :ref:`max()<class_Array_method_max>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_pick_random:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **pick_random**\ (\ ) |const| :ref:`🔗<class_Array_method_pick_random>`
|
||
|
||
从该数组中返回一个随机元素。如果数组为空,则生成一个错误并返回 ``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()<class_@GlobalScope_method_randi>` 或 :ref:`shuffle()<class_Array_method_shuffle>`\ )一样,该方法使用通用的全局随机种子。要从该方法获得可预测的结果,请参阅 :ref:`@GlobalScope.seed()<class_@GlobalScope_method_seed>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_pop_at:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **pop_at**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`🔗<class_Array_method_pop_at>`
|
||
|
||
移除并返回数组中位于 ``position`` 索引处的元素。如果 ``position`` 为负数,则认为是相对于该数组末尾的值。如果数组为空,则返回 ``null``\ ;如果 ``position`` 超出范围,还会生成错误消息。
|
||
|
||
\ **注意:**\ 该方法将 ``position`` 之后每个元素的索引向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_pop_back:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **pop_back**\ (\ ) :ref:`🔗<class_Array_method_pop_back>`
|
||
|
||
移除并返回数组中的末尾元素。如果数组为空,则返回 ``null``\ ,而不会生成错误。另见 :ref:`pop_front()<class_Array_method_pop_front>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_pop_front:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **pop_front**\ (\ ) :ref:`🔗<class_Array_method_pop_front>`
|
||
|
||
移除并返回数组的第一个元素。如果数组为空,则返回 ``null``\ ,而不会生成错误。另见 :ref:`pop_back()<class_Array_method_pop_back>`\ 。
|
||
|
||
\ **注意:**\ 该方法将每个其他元素的索引向后移动,这可能会产生明显的性能成本,尤其是在较大的数组上。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_push_back:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **push_back**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_push_back>`
|
||
|
||
在数组的末端追加一个元素。另见 :ref:`push_front()<class_Array_method_push_front>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_push_front:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **push_front**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_push_front>`
|
||
|
||
在数组的开头添加一个元素。另见 :ref:`push_back()<class_Array_method_push_back>`\ 。
|
||
|
||
\ **注意:**\ 该方法将每个其他元素的索引向前移动,这可能会产生明显的性能成本,尤其是在较大的数组上。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_reduce:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Variant<class_Variant>` **reduce**\ (\ method\: :ref:`Callable<class_Callable>`, accum\: :ref:`Variant<class_Variant>` = null\ ) |const| :ref:`🔗<class_Array_method_reduce>`
|
||
|
||
为数组中的每个元素调用给定的 :ref:`Callable<class_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()<class_Array_method_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()<class_Array_method_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()<class_Array_method_map>`\ 、\ :ref:`filter()<class_Array_method_filter>`\ 、\ :ref:`any()<class_Array_method_any>`\ 、\ :ref:`all()<class_Array_method_all>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_remove_at:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **remove_at**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`🔗<class_Array_method_remove_at>`
|
||
|
||
从数组中移除指定索引(\ ``position``\ )处的元素。如果索引超出范围,则该方法失败。如果为负数,则认为 ``position`` 为相对于数组结尾的索引。
|
||
|
||
如果需要返回被移除的元素,请使用 :ref:`pop_at()<class_Array_method_pop_at>`\ 。要按值移除元素,请改用 :ref:`erase()<class_Array_method_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<class_int>` **resize**\ (\ size\: :ref:`int<class_int>`\ ) :ref:`🔗<class_Array_method_resize>`
|
||
|
||
将数组的元素数设置为 ``size``\ 。如果 ``size`` 小于数组的当前大小,则移除末尾的元素。如果 ``size`` 大于数组的当前大小,则添加新的默认元素(通常为 ``null``\ ),具体取决于数组的类型。
|
||
|
||
如果成功则返回 :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>`\ ,如果该方法失败则返回其他 :ref:`Error<enum_@GlobalScope_Error>` 常量之一:数组只读时为 :ref:`@GlobalScope.ERR_LOCKED<class_@GlobalScope_constant_ERR_LOCKED>`\ ,大小为负数时为 :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>`\ ,分配失败时为 :ref:`@GlobalScope.ERR_OUT_OF_MEMORY<class_@GlobalScope_constant_ERR_OUT_OF_MEMORY>`\ 。请使用 :ref:`size()<class_Array_method_size>` 查看数组调整后的实际大小。
|
||
|
||
\ **注意:**\ 调用该方法一次并分配新值,要比为每个新元素调用 :ref:`append()<class_Array_method_append>` 更快。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_reverse:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **reverse**\ (\ ) :ref:`🔗<class_Array_method_reverse>`
|
||
|
||
反转数组中所有元素的顺序。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_rfind:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **rfind**\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = -1\ ) |const| :ref:`🔗<class_Array_method_rfind>`
|
||
|
||
返回该数组中 ``what`` **最后一次**\ 出现时的索引,不存在时则为 ``-1``\ 。搜索的起点可以用 ``from`` 指定,终点为该数组的开头。该方法与 :ref:`find()<class_Array_method_find>` 相对。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_rfind_custom:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **rfind_custom**\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = -1\ ) |const| :ref:`🔗<class_Array_method_rfind_custom>`
|
||
|
||
返回数组中能够让 ``method`` 返回 ``true`` 的\ **最后一个**\ 元素的索引,不存在时返回 ``-1``\ 。搜索的起点可以用 ``from`` 指定,搜索的终点为数组的开头。该方法与 :ref:`find_custom()<class_Array_method_find_custom>` 相对。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_set:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **set**\ (\ index\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Array_method_set>`
|
||
|
||
将索引为 ``index`` 的元素的值设置为 ``value``\ 。数组的大小不会发生改变,改变的只有数组中现有索引所对应的值。与使用 ``[]`` 运算符相同(\ ``array[index] = value``\ )。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_shuffle:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **shuffle**\ (\ ) :ref:`🔗<class_Array_method_shuffle>`
|
||
|
||
随机打乱数组中所有元素的顺序。
|
||
|
||
\ **注意:**\ 与引擎中很多类似的函数一样(例如 :ref:`@GlobalScope.randi()<class_@GlobalScope_method_randi>` 和 :ref:`pick_random()<class_Array_method_pick_random>`\ ),该方法使用的是通用的全局随机数种子。如何获取可预知的结果见 :ref:`@GlobalScope.seed()<class_@GlobalScope_method_seed>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_size:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`int<class_int>` **size**\ (\ ) |const| :ref:`🔗<class_Array_method_size>`
|
||
|
||
返回该数组中元素的数量。空数组(\ ``[]``\ )始终返回 ``0``\ 。另见 :ref:`is_empty()<class_Array_method_is_empty>`\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_slice:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
:ref:`Array<class_Array>` **slice**\ (\ begin\: :ref:`int<class_int>`, end\: :ref:`int<class_int>` = 2147483647, step\: :ref:`int<class_int>` = 1, deep\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`🔗<class_Array_method_slice>`
|
||
|
||
返回一个新的 **Array**\ ,其中包含该数组的元素,从索引 ``begin``\ (含)到 ``end``\ (不含),每个 ``step`` 个元素。
|
||
|
||
如果 ``begin`` 或 ``end`` 为负数,则它们的值相对于数组的末尾。
|
||
|
||
如果 ``step`` 为负数,则该方法反向遍历数组,返回按反向顺序排列的切片数组。要使其起作用,\ ``begin`` 必须大于 ``end``\ 。
|
||
|
||
如果 ``deep`` 为 ``true``\ ,则切片数组中所有嵌套的 **Array** 和 :ref:`Dictionary<class_Dictionary>` 元素都将从原始元素中递归复制。另见 :ref:`duplicate()<class_Array_method_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:`🔗<class_Array_method_sort>`
|
||
|
||
按升序对数组进行排序。最终顺序取决于元素之间的“小于”(\ ``>``\ )比较。
|
||
|
||
|
||
.. 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]
|
||
|
||
|
||
|
||
\ **注意:**\ 使用的排序算法并不\ `稳定 <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__\ 。这意味着等效元素(例如 ``2`` 和 ``2.0``\ )在调用 :ref:`sort()<class_Array_method_sort>` 时可能会改变其顺序。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_method_sort_custom:
|
||
|
||
.. rst-class:: classref-method
|
||
|
||
|void| **sort_custom**\ (\ func\: :ref:`Callable<class_Callable>`\ ) :ref:`🔗<class_Array_method_sort_custom>`
|
||
|
||
使用自定义的 :ref:`Callable<class_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()<class_String_method_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# 中,不支持该方法。
|
||
|
||
\ **注意:**\ 使用的排序算法并不是\ `稳定的 <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__\ 。这意味着在调用该方法时,被视为相等的值的顺序可能会发生变化。
|
||
|
||
\ **注意:**\ 不应随机化 ``func`` 的返回值,因为堆排序算法需要一致的结果。随机化返回值将导致意外行为。
|
||
|
||
.. rst-class:: classref-section-separator
|
||
|
||
----
|
||
|
||
.. rst-class:: classref-descriptions-group
|
||
|
||
运算符说明
|
||
----------
|
||
|
||
.. _class_Array_operator_neq_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`bool<class_bool>` **operator !=**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_neq_Array>`
|
||
|
||
如果该数组的大小或其元素与 ``right`` 不同,则返回 ``true``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_sum_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`Array<class_Array>` **operator +**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_sum_Array>`
|
||
|
||
将 ``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()<class_Array_method_append_array>` 比使用 ``+=`` 运算符的拼接和赋值效率高得多。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_lt_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`bool<class_bool>` **operator <**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_lt_Array>`
|
||
|
||
按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素小于 ``right`` 的元素,则返回 ``true``\ ;如果该元素大于 ``right`` 的元素,则返回 ``false``\ 。否则,继续下一对。
|
||
|
||
当所有搜索到的元素都相等时,如果该数组的大小小于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_lte_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`bool<class_bool>` **operator <=**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_lte_Array>`
|
||
|
||
按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素小于 ``right`` 的元素,则返回 ``true``\ ;如果该元素较大则返回 ``false``\ 。否则,继续下一对。
|
||
|
||
当所有搜索到的元素都相等时,如果该数组的大小小于或等于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_eq_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`bool<class_bool>` **operator ==**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_eq_Array>`
|
||
|
||
将左操作数 **Array** 与 ``right`` **Array** 进行比较。如果数组的大小和内容相等,则返回 ``true``\ ,否则返回 ``false``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_gt_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`bool<class_bool>` **operator >**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_gt_Array>`
|
||
|
||
按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素大于 ``right`` 的元素,则返回 ``true``\ ;如果该元素较小则返回 ``false``\ 。否则,继续下一对。
|
||
|
||
当所有搜索到的元素都相等时,如果该数组的大小大于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_gte_Array:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`bool<class_bool>` **operator >=**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`🔗<class_Array_operator_gte_Array>`
|
||
|
||
按顺序比较两个数组的元素,从索引 ``0`` 开始,到两个数组共同的最后一个索引结束。对于每对元素,如果该数组的元素大于 ``right`` 的元素,则返回 ``true``\ ,如果该元素较小则返回 ``false``\ 。否则,继续下一对。
|
||
|
||
当所有搜索到的元素都相等时,如果该数组的大小大于或等于 ``right`` 的大小,则返回 ``true``\ ,否则返回 ``false``\ 。
|
||
|
||
.. rst-class:: classref-item-separator
|
||
|
||
----
|
||
|
||
.. _class_Array_operator_idx_int:
|
||
|
||
.. rst-class:: classref-operator
|
||
|
||
:ref:`Variant<class_Variant>` **operator []**\ (\ index\: :ref:`int<class_int>`\ ) :ref:`🔗<class_Array_operator_idx_int>`
|
||
|
||
返回指定 ``index`` 处的 :ref:`Variant<class_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 (无返回值。)`
|