resize!
resize!(collection, n) -> collection
Resize collection to contain n elements.
If n is smaller than the current collection length, the first n
elements will be retained. If n is larger, the new elements are not
guaranteed to be initialized.
julia> resize!([6, 5, 4, 3, 2, 1], 3)
3-element Array{Int64,1}:
6
5
4
julia> resize!([6, 5, 4, 3, 2, 1], 8)
8-element Array{Int64,1}:
6
5
4
3
2
1
0
0Examples
In the Julia programming language, the function resize!(collection, n)
Resize collection to contain n elements. If n is smaller than the current collection length, the first n elements will be retained. If n is larger, the new elements are not guaranteed to be initialized.
julia> resize!([6, 5, 4, 3, 2, 1], 3)
3-element Array{Int64,1}:
6
5
4
julia> resize!([6, 5, 4, 3, 2, 1], 8)
8-element Array{Int64,1}:
6
5
4
3
2
1
0
0
-
Resize an array to a smaller size:
julia> arr = [10, 20, 30, 40, 50]; julia> resize!(arr, 3) 3-element Array{Int64,1}: 10 20 30This example resizes the array
arrto contain only 3 elements. The first 3 elements are retained, and the extra elements are removed. - Resize an array to a larger size:
julia> arr = [6, 5, 4, 3, 2, 1]; julia> resize!(arr, 8) 8-element Array{Int64,1}: 6 5 4 3 2 1 0 0It resizes the array
arrto contain 8 elements. If the new size is larger than the original size, the new elements are not guaranteed to be initialized.
Common mistake example:
julia> arr = [1, 2, 3, 4, 5];
julia> resize!(arr, 10)
ERROR: MethodError: no method matching resize!(::Array{Int64,1}, ::Int64)
In this example, the function resize! is called with an unsupported type. Ensure that the collection passed to resize! is of a mutable type that supports resizing.
See Also
append!, delete!, deleteat!, empty!, endof, filter, filter!, gc, get!, getkey, haskey, insert!, isempty, keys, map, map!, merge, merge!, pop!, prepend!, push!, reduce, resize!, shift!, splice!, unshift!, values,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.