Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

PackedInt32Array

32 位整数紧缩数组。

描述

专门设计用于存放 32 位整数值的数组。数据是紧密存放的,因此能够在数组较大时节省内存。

注意:该类型存储的是 32 位有符号整数,也就是说它可以取区间 [-2^31, 2^31 - 1] 内的值,即 [-2147483648, 2147483647]。超过这些界限将环绕往复。相比之下,int 使用带符号的 64 位整数,可以容纳更大的值。如果你需要紧密存放 64 位整数,请参阅 PackedInt64Array

备注

通过 C# 使用这个 API 时有显著的不同。详见 C# API 与 GDScript 的差异

构造函数

PackedInt32Array

PackedInt32Array ( )

PackedInt32Array

PackedInt32Array ( PackedInt32Array from )

PackedInt32Array

PackedInt32Array ( Array from )

方法

bool

append ( int value )

void

append_array ( PackedInt32Array array )

int

bsearch ( int value, bool before=true )

void

clear ( )

int

count ( int value ) const

PackedInt32Array

duplicate ( )

void

fill ( int value )

int

find ( int value, int from=0 ) const

bool

has ( int value ) const

int

insert ( int at_index, int value )

bool

is_empty ( ) const

bool

push_back ( int value )

void

remove_at ( int index )

int

resize ( int new_size )

void

reverse ( )

int

rfind ( int value, int from=-1 ) const

void

set ( int index, int value )

int

size ( ) const

PackedInt32Array

slice ( int begin, int end=2147483647 ) const

void

sort ( )

PackedByteArray

to_byte_array ( ) const

操作符

bool

operator != ( PackedInt32Array right )

PackedInt32Array

operator + ( PackedInt32Array right )

bool

operator == ( PackedInt32Array right )

int

operator [] ( int index )


构造函数说明

PackedInt32Array PackedInt32Array ( )

构造空的 PackedInt32Array


PackedInt32Array PackedInt32Array ( PackedInt32Array from )

构造给定 PackedInt32Array 的副本。


PackedInt32Array PackedInt32Array ( Array from )

构造新 PackedInt32Array。你还可以传入通用 Array 进行转换。


方法说明

bool append ( int value )

向数组末尾追加一个元素(push_back 的别名)。


void append_array ( PackedInt32Array array )

在该数组的末尾追加一个 PackedInt32Array


int bsearch ( int value, bool before=true )

使用二进法查找已有值的索引(如果该值尚未存在于数组中,则为保持排序顺序的插入索引)。传递 before 说明符是可选的。如果该参数为 false,则返回的索引位于数组中该值的所有已有的条目之后。

注意:在未排序的数组上调用 bsearch 会产生预料之外的行为。


void clear ( )

清空数组。相当于调用 resize 时指定大小为 0


int count ( int value ) const

返回元素在数组中出现的次数。


PackedInt32Array duplicate ( )

创建该数组的副本,并将该副本返回。


void fill ( int value )

将数组中的所有元素都设为给定的值。通常与 resize 一起使用,创建给定大小的数组并初始化元素。


int find ( int value, int from=0 ) const

在数组中搜索值并返回其索引,如果未找到则返回 -1 。可选地,可以传递起始搜索索引。


bool has ( int value ) const

如果该数组包含 value,则返回 true


int insert ( int at_index, int value )

在数组中的给定位置插入新的整数。位置必须有效,或者位于数组末尾(idx == size())。


bool is_empty ( ) const

该数组为空时,返回 true


bool push_back ( int value )

将一个值添加到数组中。


void remove_at ( int index )

从数组中删除位于索引的元素。


int resize ( int new_size )

设置数组的大小。如果数组被增大,则保留数组末端的元素。如果数组被缩小,则将数组截断到新的大小。调用一次 resize 并分配新值比逐个添加新元素要快。


void reverse ( )

将数组中的元素逆序排列。


int rfind ( int value, int from=-1 ) const

逆序搜索数组。还可以传递起始搜索位置索引。如果为负,则起始索引被视为相对于数组的结尾。


void set ( int index, int value )

更改给定索引处的整数。


int size ( ) const

返回数组中元素的个数。


PackedInt32Array slice ( int begin, int end=2147483647 ) const

返回该 PackedInt32Array 的切片,是从 begin(含)到 end(不含)的全新 PackedInt32Array

beginend 的绝对值会按数组大小进行限制,所以 end 的默认值会切到数组大小为止(即 arr.slice(1)arr.slice(1, arr.size()) 的简写)。

如果 beginend 为负,则表示相对于数组的末尾(即 arr.slice(0, -2)arr.slice(0, arr.size() - 2) 的简写)。


void sort ( )

将该数组中的元素按升序排列。


PackedByteArray to_byte_array ( ) const

返回数据的副本,将其中的每个元素都编码为 4 个字节,放入 PackedByteArray 中。

新数组的大小为 int32_array.size() * 4


操作符说明

bool operator != ( PackedInt32Array right )

如果数组内容不同,则返回 true


PackedInt32Array operator + ( PackedInt32Array right )

返回新的 PackedInt32Array,新数组的内容为此数组在末尾加上 right。为了提高性能,请考虑改用 append_array


bool operator == ( PackedInt32Array right )

如果两个数组的内容相同,即对应索引号的整数相等,则返回 true


int operator [] ( int index )

返回索引 index 处的 int。负数索引可以从末尾开始访问元素。使用超出数组范围的索引会导致出错。

注意,int 类型为 64 位,与该数组中所存储的值不同。