Vectors are one-dimensional arrays, and support mostly the same interface as their multi-dimensional counterparts. However, vectors also support additional operations.
First, note that Vector{T}
where T
is some type means the same as Array{T,1}
.
julia> Vector{Int}
Array{Int64,1}
julia> Vector{Float64}
Array{Float64,1}
One reads Array{Int64,1}
as "one-dimensional array of Int64
".
Unlike multi-dimensional arrays, vectors can be resized. Elements can be added or removed from the front or back of the vector. These operations are all constant amortized time.
julia> A = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
julia> push!(A, 4)
4-element Array{Int64,1}:
1
2
3
4
julia> A
4-element Array{Int64,1}:
1
2
3
4
julia> pop!(A)
4
julia> A
3-element Array{Int64,1}:
1
2
3
julia> unshift!(A, 0)
4-element Array{Int64,1}:
0
1
2
3
julia> A
4-element Array{Int64,1}:
0
1
2
3
julia> shift!(A)
0
julia> A
3-element Array{Int64,1}:
1
2
3
As is convention, each of these functions push!
, pop!
, unshift!
, and shift!
ends in an exclamation mark to indicate that they are mutate their argument. The functions push!
and unshift!
return the array, whereas pop!
and shift!
return the element removed.