:github_url: hide .. _class_PackedInt32Array: PackedInt32Array ================ 32 位整数紧缩数组。 .. rst-class:: classref-introduction-group 描述 ---- 专门设计用于存放 32 位整数值的数组。数据是紧密存放的,因此能够在数组较大时节省内存。 \ **注意:**\ 该类型存储的是 32 位有符号整数,也就是说它可以取区间 ``[-2^31, 2^31 - 1]`` 内的值,即 ``[-2147483648, 2147483647]``\ 。超过界限时会从另一端绕回。相比之下,\ :ref:`int` 使用带符号的 64 位整数,可以容纳更大的值。紧密存放 64 位整数见 :ref:`PackedInt64Array`\ 。 \ **注意:**\ 紧缩数组始终通过引用传递。要获取可以独立于原始数组进行修改的数组副本,请使用 :ref:`duplicate()`\ 。内置属性和方法\ *并非*\ 如此,它们返回的是紧缩数组的副本,对其进行修改\ *不会*\ 影响原值。更新此类内置属性请修改返回的数组,然后将其重新赋值给该属性。 .. note:: 通过 C# 使用该 API 时会有显著不同,详见 :ref:`doc_c_sharp_differences`\ 。 .. rst-class:: classref-reftable-group 构造函数 -------- .. table:: :widths: auto +-------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`PackedInt32Array`\ (\ ) | +-------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`PackedInt32Array`\ (\ from\: :ref:`PackedInt32Array`\ ) | +-------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`PackedInt32Array`\ (\ from\: :ref:`Array`\ ) | +-------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`append`\ (\ value\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`append_array`\ (\ array\: :ref:`PackedInt32Array`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`bsearch`\ (\ value\: :ref:`int`, before\: :ref:`bool` = true\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`clear`\ (\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`count`\ (\ value\: :ref:`int`\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`duplicate`\ (\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`erase`\ (\ value\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`fill`\ (\ value\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`find`\ (\ value\: :ref:`int`, from\: :ref:`int` = 0\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get`\ (\ index\: :ref:`int`\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has`\ (\ value\: :ref:`int`\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`insert`\ (\ at_index\: :ref:`int`, value\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_empty`\ (\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`push_back`\ (\ value\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`remove_at`\ (\ index\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`resize`\ (\ new_size\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`reverse`\ (\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`rfind`\ (\ value\: :ref:`int`, from\: :ref:`int` = -1\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set`\ (\ index\: :ref:`int`, value\: :ref:`int`\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`size`\ (\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`slice`\ (\ begin\: :ref:`int`, end\: :ref:`int` = 2147483647\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`sort`\ (\ ) | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedByteArray` | :ref:`to_byte_array`\ (\ ) |const| | +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group 运算符 ------ .. table:: :widths: auto +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator !=`\ (\ right\: :ref:`PackedInt32Array`\ ) | +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`PackedInt32Array` | :ref:`operator +`\ (\ right\: :ref:`PackedInt32Array`\ ) | +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator ==`\ (\ right\: :ref:`PackedInt32Array`\ ) | +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`operator []`\ (\ index\: :ref:`int`\ ) | +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 构造函数说明 ------------ .. _class_PackedInt32Array_constructor_PackedInt32Array: .. rst-class:: classref-constructor :ref:`PackedInt32Array` **PackedInt32Array**\ (\ ) :ref:`🔗` 构造空的 **PackedInt32Array**\ 。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`PackedInt32Array` **PackedInt32Array**\ (\ from\: :ref:`PackedInt32Array`\ ) 构造给定 **PackedInt32Array** 的副本。 .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`PackedInt32Array` **PackedInt32Array**\ (\ from\: :ref:`Array`\ ) 构造新 **PackedInt32Array**\ 。你还可以传入通用 :ref:`Array` 进行转换。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法说明 -------- .. _class_PackedInt32Array_method_append: .. rst-class:: classref-method :ref:`bool` **append**\ (\ value\: :ref:`int`\ ) :ref:`🔗` 向数组末尾追加一个元素(\ :ref:`push_back()` 的别名)。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_append_array: .. rst-class:: classref-method |void| **append_array**\ (\ array\: :ref:`PackedInt32Array`\ ) :ref:`🔗` 在该数组的末尾追加一个 **PackedInt32Array**\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_bsearch: .. rst-class:: classref-method :ref:`int` **bsearch**\ (\ value\: :ref:`int`, before\: :ref:`bool` = true\ ) |const| :ref:`🔗` 使用二进法查找已有值的索引(如果该值尚未存在于数组中,则为保持排序顺序的插入索引)。传递 ``before`` 说明符是可选的。如果该参数为 ``false``\ ,则返回的索引位于数组中该值的所有已有的条目之后。 \ **注意:**\ 在未排序的数组上调用 :ref:`bsearch()` 会产生预料之外的行为。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_clear: .. rst-class:: classref-method |void| **clear**\ (\ ) :ref:`🔗` 清空数组。相当于调用 :ref:`resize()` 时指定大小为 ``0``\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_count: .. rst-class:: classref-method :ref:`int` **count**\ (\ value\: :ref:`int`\ ) |const| :ref:`🔗` 返回元素在数组中出现的次数。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_duplicate: .. rst-class:: classref-method :ref:`PackedInt32Array` **duplicate**\ (\ ) |const| :ref:`🔗` 创建该数组的副本,并将该副本返回。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_erase: .. rst-class:: classref-method :ref:`bool` **erase**\ (\ value\: :ref:`int`\ ) :ref:`🔗` 在数组中移除首次出现的某个值并返回 ``true``\ 。如果数组中不存在该值,则不会发生任何事情,返回 ``false``\ 。要按照索引移除元素,请改用 :ref:`remove_at()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_fill: .. rst-class:: classref-method |void| **fill**\ (\ value\: :ref:`int`\ ) :ref:`🔗` 将数组中的所有元素都设为给定的值。通常与 :ref:`resize()` 一起使用,创建给定大小的数组并初始化元素。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_find: .. rst-class:: classref-method :ref:`int` **find**\ (\ value\: :ref:`int`, from\: :ref:`int` = 0\ ) |const| :ref:`🔗` 在数组中搜索值并返回其索引,如果未找到则返回 ``-1`` 。可选地,可以传递起始搜索索引。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_get: .. rst-class:: classref-method :ref:`int` **get**\ (\ index\: :ref:`int`\ ) |const| :ref:`🔗` Returns the 32-bit integer at the given ``index`` in the array. If ``index`` is out-of-bounds or negative, this method fails and returns ``0``. This method is similar (but not identical) to the ``[]`` operator. Most notably, when this method fails, it doesn't pause project execution if run from the editor. .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_has: .. rst-class:: classref-method :ref:`bool` **has**\ (\ value\: :ref:`int`\ ) |const| :ref:`🔗` 如果该数组包含 ``value``\ ,则返回 ``true``\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_insert: .. rst-class:: classref-method :ref:`int` **insert**\ (\ at_index\: :ref:`int`, value\: :ref:`int`\ ) :ref:`🔗` 在数组中的给定位置插入新的整数。位置必须有效,或者位于数组末尾(\ ``idx == size()``\ )。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_is_empty: .. rst-class:: classref-method :ref:`bool` **is_empty**\ (\ ) |const| :ref:`🔗` 该数组为空时,返回 ``true``\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_push_back: .. rst-class:: classref-method :ref:`bool` **push_back**\ (\ value\: :ref:`int`\ ) :ref:`🔗` 将一个值添加到数组中。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_remove_at: .. rst-class:: classref-method |void| **remove_at**\ (\ index\: :ref:`int`\ ) :ref:`🔗` 从数组中删除位于索引的元素。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_resize: .. rst-class:: classref-method :ref:`int` **resize**\ (\ new_size\: :ref:`int`\ ) :ref:`🔗` 设置数组的大小。如果数组增大,则预留数组末端的元素。如果数组缩小,则将数组截断到新的大小。调用一次 :ref:`resize()` 然后赋值比逐个添加新元素要快。 成功时返回 :ref:`@GlobalScope.OK`\ ,失败时返回下列 :ref:`Error` 常量:大小为负数则返回 :ref:`@GlobalScope.ERR_INVALID_PARAMETER`\ ,分配失败则返回 :ref:`@GlobalScope.ERR_OUT_OF_MEMORY`\ 。请使用 :ref:`size()` 获取调整后的实际大小。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_reverse: .. rst-class:: classref-method |void| **reverse**\ (\ ) :ref:`🔗` 将数组中的元素逆序排列。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_rfind: .. rst-class:: classref-method :ref:`int` **rfind**\ (\ value\: :ref:`int`, from\: :ref:`int` = -1\ ) |const| :ref:`🔗` 逆序搜索数组。还可以传递起始搜索位置索引。如果为负,则起始索引被视为相对于数组的结尾。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_set: .. rst-class:: classref-method |void| **set**\ (\ index\: :ref:`int`, value\: :ref:`int`\ ) :ref:`🔗` 更改给定索引处的整数。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_size: .. rst-class:: classref-method :ref:`int` **size**\ (\ ) |const| :ref:`🔗` 返回数组中元素的个数。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_slice: .. rst-class:: classref-method :ref:`PackedInt32Array` **slice**\ (\ begin\: :ref:`int`, end\: :ref:`int` = 2147483647\ ) |const| :ref:`🔗` 返回该 **PackedInt32Array** 的切片,是从 ``begin``\ (含)到 ``end``\ (不含)的全新 **PackedInt32Array**\ 。 \ ``begin`` 和 ``end`` 的绝对值会按数组大小进行限制,所以 ``end`` 的默认值会切到数组大小为止(即 ``arr.slice(1)`` 是 ``arr.slice(1, arr.size())`` 的简写)。 如果 ``begin`` 或 ``end`` 为负,则表示相对于数组的末尾(即 ``arr.slice(0, -2)`` 是 ``arr.slice(0, arr.size() - 2)`` 的简写)。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_sort: .. rst-class:: classref-method |void| **sort**\ (\ ) :ref:`🔗` 将该数组中的元素按升序排列。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_method_to_byte_array: .. rst-class:: classref-method :ref:`PackedByteArray` **to_byte_array**\ (\ ) |const| :ref:`🔗` 返回数据的副本,将其中的每个元素都编码为 4 个字节,放入 :ref:`PackedByteArray` 中。 新数组的大小为 ``int32_array.size() * 4``\ 。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 运算符说明 ---------- .. _class_PackedInt32Array_operator_neq_PackedInt32Array: .. rst-class:: classref-operator :ref:`bool` **operator !=**\ (\ right\: :ref:`PackedInt32Array`\ ) :ref:`🔗` 如果数组内容不同,则返回 ``true``\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_operator_sum_PackedInt32Array: .. rst-class:: classref-operator :ref:`PackedInt32Array` **operator +**\ (\ right\: :ref:`PackedInt32Array`\ ) :ref:`🔗` 返回新的 **PackedInt32Array**\ ,新数组的内容为此数组在末尾加上 ``right``\ 。为了提高性能,请考虑改用 :ref:`append_array()`\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_operator_eq_PackedInt32Array: .. rst-class:: classref-operator :ref:`bool` **operator ==**\ (\ right\: :ref:`PackedInt32Array`\ ) :ref:`🔗` 如果两个数组的内容相同,即对应索引号的整数相等,则返回 ``true``\ 。 .. rst-class:: classref-item-separator ---- .. _class_PackedInt32Array_operator_idx_int: .. rst-class:: classref-operator :ref:`int` **operator []**\ (\ index\: :ref:`int`\ ) :ref:`🔗` 返回索引 ``index`` 处的 :ref:`int`\ 。负数索引可以从末尾开始访问元素。使用超出数组范围的索引会导致出错。 注意,\ :ref:`int` 类型为 64 位,与该数组中所存储的值不同。 .. |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 (无返回值。)`