class_array.rst 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.5/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.5/doc/classes/Array.xml.
  6. .. _class_Array:
  7. Array
  8. =====
  9. A generic array datatype.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. A generic array that can contain several elements of any type, accessible by a numerical index starting at 0. Negative indices can be used to count from the back, like in Python (-1 is the last element, -2 is the second to last, etc.).
  14. \ **Example:**\
  15. ::
  16. var array = ["One", 2, 3, "Four"]
  17. print(array[0]) # One.
  18. print(array[2]) # 3.
  19. print(array[-1]) # Four.
  20. array[2] = "Three"
  21. print(array[-2]) # Three.
  22. Arrays can be concatenated using the ``+`` operator:
  23. ::
  24. var array1 = ["One", 2]
  25. var array2 = [3, "Four"]
  26. print(array1 + array2) # ["One", 2, 3, "Four"]
  27. \ **Note:** Concatenating with the ``+=`` operator will create a new array, which has a cost. If you want to append another array to an existing array, :ref:`append_array<class_Array_method_append_array>` is more efficient.
  28. \ **Note:** Arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use :ref:`duplicate<class_Array_method_duplicate>`.
  29. \ **Note:** When declaring an array with ``const``, the array itself can still be mutated by defining the values at individual indices or pushing/removing elements. Using ``const`` will only prevent assigning the constant with another value after it was initialized.
  30. .. rst-class:: classref-reftable-group
  31. Methods
  32. -------
  33. .. table::
  34. :widths: auto
  35. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolColorArray<class_PoolColorArray>` from **)** |
  37. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolVector3Array<class_PoolVector3Array>` from **)** |
  39. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolVector2Array<class_PoolVector2Array>` from **)** |
  41. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolStringArray<class_PoolStringArray>` from **)** |
  43. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolRealArray<class_PoolRealArray>` from **)** |
  45. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolIntArray<class_PoolIntArray>` from **)** |
  47. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolByteArray<class_PoolByteArray>` from **)** |
  49. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | void | :ref:`append<class_Array_method_append>` **(** :ref:`Variant<class_Variant>` value **)** |
  51. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | void | :ref:`append_array<class_Array_method_append_array>` **(** :ref:`Array<class_Array>` array **)** |
  53. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`Variant<class_Variant>` | :ref:`back<class_Array_method_back>` **(** **)** |
  55. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`int<class_int>` | :ref:`bsearch<class_Array_method_bsearch>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)** |
  57. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_method_bsearch_custom>` **(** :ref:`Variant<class_Variant>` value, :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func, :ref:`bool<class_bool>` before=true **)** |
  59. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | void | :ref:`clear<class_Array_method_clear>` **(** **)** |
  61. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`int<class_int>` | :ref:`count<class_Array_method_count>` **(** :ref:`Variant<class_Variant>` value **)** |
  63. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Array<class_Array>` | :ref:`duplicate<class_Array_method_duplicate>` **(** :ref:`bool<class_bool>` deep=false **)** |
  65. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`bool<class_bool>` | :ref:`empty<class_Array_method_empty>` **(** **)** |
  67. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | void | :ref:`erase<class_Array_method_erase>` **(** :ref:`Variant<class_Variant>` value **)** |
  69. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | void | :ref:`fill<class_Array_method_fill>` **(** :ref:`Variant<class_Variant>` value **)** |
  71. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`int<class_int>` | :ref:`find<class_Array_method_find>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)** |
  73. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`int<class_int>` | :ref:`find_last<class_Array_method_find_last>` **(** :ref:`Variant<class_Variant>` value **)** |
  75. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`Variant<class_Variant>` | :ref:`front<class_Array_method_front>` **(** **)** |
  77. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | :ref:`bool<class_bool>` | :ref:`has<class_Array_method_has>` **(** :ref:`Variant<class_Variant>` value **)** |
  79. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :ref:`int<class_int>` | :ref:`hash<class_Array_method_hash>` **(** **)** |
  81. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | void | :ref:`insert<class_Array_method_insert>` **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)** |
  83. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | void | :ref:`invert<class_Array_method_invert>` **(** **)** |
  85. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`Variant<class_Variant>` | :ref:`max<class_Array_method_max>` **(** **)** |
  87. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`Variant<class_Variant>` | :ref:`min<class_Array_method_min>` **(** **)** |
  89. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | :ref:`Variant<class_Variant>` | :ref:`pop_at<class_Array_method_pop_at>` **(** :ref:`int<class_int>` position **)** |
  91. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | :ref:`Variant<class_Variant>` | :ref:`pop_back<class_Array_method_pop_back>` **(** **)** |
  93. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | :ref:`Variant<class_Variant>` | :ref:`pop_front<class_Array_method_pop_front>` **(** **)** |
  95. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | void | :ref:`push_back<class_Array_method_push_back>` **(** :ref:`Variant<class_Variant>` value **)** |
  97. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | void | :ref:`push_front<class_Array_method_push_front>` **(** :ref:`Variant<class_Variant>` value **)** |
  99. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. | void | :ref:`remove<class_Array_method_remove>` **(** :ref:`int<class_int>` position **)** |
  101. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. | void | :ref:`resize<class_Array_method_resize>` **(** :ref:`int<class_int>` size **)** |
  103. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  104. | :ref:`int<class_int>` | :ref:`rfind<class_Array_method_rfind>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)** |
  105. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  106. | void | :ref:`shuffle<class_Array_method_shuffle>` **(** **)** |
  107. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  108. | :ref:`int<class_int>` | :ref:`size<class_Array_method_size>` **(** **)** |
  109. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  110. | :ref:`Array<class_Array>` | :ref:`slice<class_Array_method_slice>` **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)** |
  111. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  112. | void | :ref:`sort<class_Array_method_sort>` **(** **)** |
  113. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  114. | void | :ref:`sort_custom<class_Array_method_sort_custom>` **(** :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func **)** |
  115. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  116. .. rst-class:: classref-section-separator
  117. ----
  118. .. rst-class:: classref-descriptions-group
  119. Method Descriptions
  120. -------------------
  121. .. _class_Array_method_Array:
  122. .. rst-class:: classref-method
  123. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolColorArray<class_PoolColorArray>` from **)**
  124. Constructs an array from a :ref:`PoolColorArray<class_PoolColorArray>`.
  125. .. rst-class:: classref-item-separator
  126. ----
  127. .. rst-class:: classref-method
  128. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolVector3Array<class_PoolVector3Array>` from **)**
  129. Constructs an array from a :ref:`PoolVector3Array<class_PoolVector3Array>`.
  130. .. rst-class:: classref-item-separator
  131. ----
  132. .. rst-class:: classref-method
  133. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolVector2Array<class_PoolVector2Array>` from **)**
  134. Constructs an array from a :ref:`PoolVector2Array<class_PoolVector2Array>`.
  135. .. rst-class:: classref-item-separator
  136. ----
  137. .. rst-class:: classref-method
  138. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolStringArray<class_PoolStringArray>` from **)**
  139. Constructs an array from a :ref:`PoolStringArray<class_PoolStringArray>`.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. rst-class:: classref-method
  143. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolRealArray<class_PoolRealArray>` from **)**
  144. Constructs an array from a :ref:`PoolRealArray<class_PoolRealArray>`.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. rst-class:: classref-method
  148. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolIntArray<class_PoolIntArray>` from **)**
  149. Constructs an array from a :ref:`PoolIntArray<class_PoolIntArray>`.
  150. .. rst-class:: classref-item-separator
  151. ----
  152. .. rst-class:: classref-method
  153. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolByteArray<class_PoolByteArray>` from **)**
  154. Constructs an array from a :ref:`PoolByteArray<class_PoolByteArray>`.
  155. .. rst-class:: classref-item-separator
  156. ----
  157. .. _class_Array_method_append:
  158. .. rst-class:: classref-method
  159. void **append** **(** :ref:`Variant<class_Variant>` value **)**
  160. Appends an element at the end of the array (alias of :ref:`push_back<class_Array_method_push_back>`).
  161. .. rst-class:: classref-item-separator
  162. ----
  163. .. _class_Array_method_append_array:
  164. .. rst-class:: classref-method
  165. void **append_array** **(** :ref:`Array<class_Array>` array **)**
  166. Appends another array at the end of this array.
  167. ::
  168. var array1 = [1, 2, 3]
  169. var array2 = [4, 5, 6]
  170. array1.append_array(array2)
  171. print(array1) # Prints [1, 2, 3, 4, 5, 6].
  172. .. rst-class:: classref-item-separator
  173. ----
  174. .. _class_Array_method_back:
  175. .. rst-class:: classref-method
  176. :ref:`Variant<class_Variant>` **back** **(** **)**
  177. Returns the last element of the array. Prints an error and returns ``null`` if the array is empty.
  178. \ **Note:** Calling this function is not the same as writing ``array[-1]``. If the array is empty, accessing by index will pause project execution when running from the editor.
  179. .. rst-class:: classref-item-separator
  180. ----
  181. .. _class_Array_method_bsearch:
  182. .. rst-class:: classref-method
  183. :ref:`int<class_int>` **bsearch** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)**
  184. Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search. Optionally, a ``before`` specifier can be passed. If ``false``, the returned index comes after all existing entries of the value in the array.
  185. \ **Note:** Calling :ref:`bsearch<class_Array_method_bsearch>` on an unsorted array results in unexpected behavior.
  186. .. rst-class:: classref-item-separator
  187. ----
  188. .. _class_Array_method_bsearch_custom:
  189. .. rst-class:: classref-method
  190. :ref:`int<class_int>` **bsearch_custom** **(** :ref:`Variant<class_Variant>` value, :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func, :ref:`bool<class_bool>` before=true **)**
  191. Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search and a custom comparison method declared in the ``obj``. Optionally, a ``before`` specifier can be passed. If ``false``, the returned index comes after all existing entries of the value in the array. The custom method receives two arguments (an element from the array and the value searched for) and must return ``true`` if the first argument is less than the second, and return ``false`` otherwise.
  192. ::
  193. func cardinal_to_algebraic(a):
  194. match a:
  195. "one":
  196. return 1
  197. "two":
  198. return 2
  199. "three":
  200. return 3
  201. "four":
  202. return 4
  203. _:
  204. return 0
  205. func compare(a, b):
  206. return cardinal_to_algebraic(a) < cardinal_to_algebraic(b)
  207. func _ready():
  208. var a = ["one", "two", "three", "four"]
  209. # `compare` is defined in this object, so we use `self` as the `obj` parameter.
  210. print(a.bsearch_custom("three", self, "compare", true)) # Expected value is 2.
  211. \ **Note:** Calling :ref:`bsearch_custom<class_Array_method_bsearch_custom>` on an unsorted array results in unexpected behavior.
  212. .. rst-class:: classref-item-separator
  213. ----
  214. .. _class_Array_method_clear:
  215. .. rst-class:: classref-method
  216. void **clear** **(** **)**
  217. Clears the array. This is equivalent to using :ref:`resize<class_Array_method_resize>` with a size of ``0``.
  218. .. rst-class:: classref-item-separator
  219. ----
  220. .. _class_Array_method_count:
  221. .. rst-class:: classref-method
  222. :ref:`int<class_int>` **count** **(** :ref:`Variant<class_Variant>` value **)**
  223. Returns the number of times an element is in the array.
  224. .. rst-class:: classref-item-separator
  225. ----
  226. .. _class_Array_method_duplicate:
  227. .. rst-class:: classref-method
  228. :ref:`Array<class_Array>` **duplicate** **(** :ref:`bool<class_bool>` deep=false **)**
  229. Returns a copy of the array.
  230. If ``deep`` is ``true``, a deep copy is performed: all nested arrays and dictionaries are duplicated and will not be shared with the original array. If ``false``, a shallow copy is made and references to the original nested arrays and dictionaries are kept, so that modifying a sub-array or dictionary in the copy will also impact those referenced in the source array.
  231. .. rst-class:: classref-item-separator
  232. ----
  233. .. _class_Array_method_empty:
  234. .. rst-class:: classref-method
  235. :ref:`bool<class_bool>` **empty** **(** **)**
  236. Returns ``true`` if the array is empty.
  237. .. rst-class:: classref-item-separator
  238. ----
  239. .. _class_Array_method_erase:
  240. .. rst-class:: classref-method
  241. void **erase** **(** :ref:`Variant<class_Variant>` value **)**
  242. Removes the first occurrence of a value from the array. If the value does not exist in the array, nothing happens. To remove an element by index, use :ref:`remove<class_Array_method_remove>` instead.
  243. \ **Note:** This method acts in-place and doesn't return a value.
  244. \ **Note:** On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
  245. .. rst-class:: classref-item-separator
  246. ----
  247. .. _class_Array_method_fill:
  248. .. rst-class:: classref-method
  249. void **fill** **(** :ref:`Variant<class_Variant>` value **)**
  250. Assigns the given value to all elements in the array. This can typically be used together with :ref:`resize<class_Array_method_resize>` to create an array with a given size and initialized elements:
  251. ::
  252. var array = []
  253. array.resize(10)
  254. array.fill(0) # Initialize the 10 elements to 0.
  255. \ **Note:** If ``value`` is of a reference type (:ref:`Object<class_Object>`-derived, **Array**, :ref:`Dictionary<class_Dictionary>`, etc.) then the array is filled with the references to the same object, i.e. no duplicates are created.
  256. .. rst-class:: classref-item-separator
  257. ----
  258. .. _class_Array_method_find:
  259. .. rst-class:: classref-method
  260. :ref:`int<class_int>` **find** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)**
  261. Searches the array for a value and returns its index or ``-1`` if not found. Optionally, the initial search index can be passed. Returns ``-1`` if ``from`` is out of bounds.
  262. .. rst-class:: classref-item-separator
  263. ----
  264. .. _class_Array_method_find_last:
  265. .. rst-class:: classref-method
  266. :ref:`int<class_int>` **find_last** **(** :ref:`Variant<class_Variant>` value **)**
  267. Searches the array in reverse order for a value and returns its index or ``-1`` if not found.
  268. .. rst-class:: classref-item-separator
  269. ----
  270. .. _class_Array_method_front:
  271. .. rst-class:: classref-method
  272. :ref:`Variant<class_Variant>` **front** **(** **)**
  273. Returns the first element of the array. Prints an error and returns ``null`` if the array is empty.
  274. \ **Note:** Calling this function is not the same as writing ``array[0]``. If the array is empty, accessing by index will pause project execution when running from the editor.
  275. .. rst-class:: classref-item-separator
  276. ----
  277. .. _class_Array_method_has:
  278. .. rst-class:: classref-method
  279. :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` value **)**
  280. Returns ``true`` if the array contains the given value.
  281. ::
  282. ["inside", 7].has("inside") # True
  283. ["inside", 7].has("outside") # False
  284. ["inside", 7].has(7) # True
  285. ["inside", 7].has("7") # False
  286. \ **Note:** This is equivalent to using the ``in`` operator as follows:
  287. ::
  288. # Will evaluate to `true`.
  289. if 2 in [2, 4, 6, 8]:
  290. pass
  291. .. rst-class:: classref-item-separator
  292. ----
  293. .. _class_Array_method_hash:
  294. .. rst-class:: classref-method
  295. :ref:`int<class_int>` **hash** **(** **)**
  296. Returns a hashed 32-bit integer value representing the array and its contents.
  297. \ **Note:** **Array**\ s with equal content will always produce identical hash values. However, the reverse is not true. Returning identical hash values does *not* imply the arrays are equal, because different arrays can have identical hash values due to hash collisions.
  298. .. rst-class:: classref-item-separator
  299. ----
  300. .. _class_Array_method_insert:
  301. .. rst-class:: classref-method
  302. void **insert** **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)**
  303. Inserts a new element at a given position in the array. The position must be valid, or at the end of the array (``pos == size()``).
  304. \ **Note:** This method acts in-place and doesn't return a value.
  305. \ **Note:** On large arrays, this method will be slower if the inserted element is close to the beginning of the array (index 0). This is because all elements placed after the newly inserted element have to be reindexed.
  306. .. rst-class:: classref-item-separator
  307. ----
  308. .. _class_Array_method_invert:
  309. .. rst-class:: classref-method
  310. void **invert** **(** **)**
  311. Reverses the order of the elements in the array.
  312. .. rst-class:: classref-item-separator
  313. ----
  314. .. _class_Array_method_max:
  315. .. rst-class:: classref-method
  316. :ref:`Variant<class_Variant>` **max** **(** **)**
  317. Returns the maximum value contained in the array if all elements are of comparable types. If the elements can't be compared, ``null`` is returned.
  318. .. rst-class:: classref-item-separator
  319. ----
  320. .. _class_Array_method_min:
  321. .. rst-class:: classref-method
  322. :ref:`Variant<class_Variant>` **min** **(** **)**
  323. Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, ``null`` is returned.
  324. .. rst-class:: classref-item-separator
  325. ----
  326. .. _class_Array_method_pop_at:
  327. .. rst-class:: classref-method
  328. :ref:`Variant<class_Variant>` **pop_at** **(** :ref:`int<class_int>` position **)**
  329. Removes and returns the element of the array at index ``position``. If negative, ``position`` is considered relative to the end of the array. Leaves the array untouched and returns ``null`` if the array is empty or if it's accessed out of bounds. An error message is printed when the array is accessed out of bounds, but not when the array is empty.
  330. \ **Note:** On large arrays, this method can be slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex the array's elements that are located after the removed element. The larger the array and the lower the index of the removed element, the slower :ref:`pop_at<class_Array_method_pop_at>` will be.
  331. .. rst-class:: classref-item-separator
  332. ----
  333. .. _class_Array_method_pop_back:
  334. .. rst-class:: classref-method
  335. :ref:`Variant<class_Variant>` **pop_back** **(** **)**
  336. Removes and returns the last element of the array. Returns ``null`` if the array is empty, without printing an error message. See also :ref:`pop_front<class_Array_method_pop_front>`.
  337. .. rst-class:: classref-item-separator
  338. ----
  339. .. _class_Array_method_pop_front:
  340. .. rst-class:: classref-method
  341. :ref:`Variant<class_Variant>` **pop_front** **(** **)**
  342. Removes and returns the first element of the array. Returns ``null`` if the array is empty, without printing an error message. See also :ref:`pop_back<class_Array_method_pop_back>`.
  343. \ **Note:** On large arrays, this method is much slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`pop_front<class_Array_method_pop_front>` will be.
  344. .. rst-class:: classref-item-separator
  345. ----
  346. .. _class_Array_method_push_back:
  347. .. rst-class:: classref-method
  348. void **push_back** **(** :ref:`Variant<class_Variant>` value **)**
  349. Appends an element at the end of the array. See also :ref:`push_front<class_Array_method_push_front>`.
  350. .. rst-class:: classref-item-separator
  351. ----
  352. .. _class_Array_method_push_front:
  353. .. rst-class:: classref-method
  354. void **push_front** **(** :ref:`Variant<class_Variant>` value **)**
  355. Adds an element at the beginning of the array. See also :ref:`push_back<class_Array_method_push_back>`.
  356. \ **Note:** On large arrays, this method is much slower than :ref:`push_back<class_Array_method_push_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`push_front<class_Array_method_push_front>` will be.
  357. .. rst-class:: classref-item-separator
  358. ----
  359. .. _class_Array_method_remove:
  360. .. rst-class:: classref-method
  361. void **remove** **(** :ref:`int<class_int>` position **)**
  362. Removes an element from the array by index. If the index does not exist in the array, nothing happens. To remove an element by searching for its value, use :ref:`erase<class_Array_method_erase>` instead.
  363. \ **Note:** This method acts in-place and doesn't return a value.
  364. \ **Note:** On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
  365. .. rst-class:: classref-item-separator
  366. ----
  367. .. _class_Array_method_resize:
  368. .. rst-class:: classref-method
  369. void **resize** **(** :ref:`int<class_int>` size **)**
  370. Resizes the array to contain a different number of elements. If the array size is smaller, elements are cleared, if bigger, new elements are ``null``.
  371. .. rst-class:: classref-item-separator
  372. ----
  373. .. _class_Array_method_rfind:
  374. .. rst-class:: classref-method
  375. :ref:`int<class_int>` **rfind** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)**
  376. Searches the array in reverse order. Optionally, a start search index can be passed. If negative, the start index is considered relative to the end of the array. If the adjusted start index is out of bounds, this method searches from the end of the array.
  377. .. rst-class:: classref-item-separator
  378. ----
  379. .. _class_Array_method_shuffle:
  380. .. rst-class:: classref-method
  381. void **shuffle** **(** **)**
  382. Shuffles the array such that the items will have a random order. This method uses the global random number generator common to methods such as :ref:`@GDScript.randi<class_@GDScript_method_randi>`. Call :ref:`@GDScript.randomize<class_@GDScript_method_randomize>` to ensure that a new seed will be used each time if you want non-reproducible shuffling.
  383. .. rst-class:: classref-item-separator
  384. ----
  385. .. _class_Array_method_size:
  386. .. rst-class:: classref-method
  387. :ref:`int<class_int>` **size** **(** **)**
  388. Returns the number of elements in the array.
  389. .. rst-class:: classref-item-separator
  390. ----
  391. .. _class_Array_method_slice:
  392. .. rst-class:: classref-method
  393. :ref:`Array<class_Array>` **slice** **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)**
  394. Duplicates the subset described in the function and returns it in an array, deeply copying the array if ``deep`` is ``true``. Lower and upper index are inclusive, with the ``step`` describing the change between indices while slicing.
  395. .. rst-class:: classref-item-separator
  396. ----
  397. .. _class_Array_method_sort:
  398. .. rst-class:: classref-method
  399. void **sort** **(** **)**
  400. Sorts the array.
  401. \ **Note:** The sorting algorithm used is not `stable <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__. This means that values considered equal may have their order changed when using :ref:`sort<class_Array_method_sort>`.
  402. \ **Note:** Strings are sorted in alphabetical order (as opposed to natural order). This may lead to unexpected behavior when sorting an array of strings ending with a sequence of numbers. Consider the following example:
  403. ::
  404. var strings = ["string1", "string2", "string10", "string11"]
  405. strings.sort()
  406. print(strings) # Prints [string1, string10, string11, string2]
  407. .. rst-class:: classref-item-separator
  408. ----
  409. .. _class_Array_method_sort_custom:
  410. .. rst-class:: classref-method
  411. void **sort_custom** **(** :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func **)**
  412. Sorts the array using a custom method. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return either ``true`` or ``false``.
  413. For two elements ``a`` and ``b``, if the given method returns ``true``, element ``b`` will be after element ``a`` in the array.
  414. \ **Note:** The sorting algorithm used is not `stable <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__. This means that values considered equal may have their order changed when using :ref:`sort_custom<class_Array_method_sort_custom>`.
  415. \ **Note:** You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Randomizing the return value will result in unexpected behavior.
  416. ::
  417. class MyCustomSorter:
  418. static func sort_ascending(a, b):
  419. if a[0] < b[0]:
  420. return true
  421. return false
  422. var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
  423. my_items.sort_custom(MyCustomSorter, "sort_ascending")
  424. print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]].
  425. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  426. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  427. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  428. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`