Julia Arrays
An array is an ordered, mutable collection stored in one block of memory. A one-dimensional array is called a vector (Vector{T}), a two-dimensional one a matrix (Matrix{T}). Arrays are at the heart of scientific Julia code.
Creating vectors
nums = [10, 20, 30]
mixed = [1, 2.5, 3] # promoted to a common type
words = ["a", "b"]
anything = [1, "two", 3.0] # no common type: Vector{Any}
empty_ints = Int[] # empty, with an element type
println(nums, " :: ", typeof(nums))
println(mixed, " :: ", typeof(mixed))
println(typeof(anything), " ", typeof(empty_ints))
println(zeros(3), " ", ones(Int, 3), " ", fill("x", 2))
println(collect(1:5), " ", collect(range(0, 1, length=5)))[10, 20, 30] :: Vector{Int64}
[1.0, 2.5, 3.0] :: Vector{Float64}
Vector{Any} Vector{Int64}
[0.0, 0.0, 0.0] [1, 1, 1] ["x", "x"]
[1, 2, 3, 4, 5] [0.0, 0.25, 0.5, 0.75, 1.0]
Julia picks the most specific element type that fits all items. Arrays with a concrete element type like Float64 are much faster than Vector{Any}.
Indexing
Indices start at 1. end refers to the last index, and an array or range of indices returns a new array:
v = [5, 10, 15, 20, 25]
println(v[1], " ", v[end], " ", v[end-1])
println(v[2:4])
println(v[[1, 3, 5]])
println(v[v .> 12]) # logical indexing with a Bool mask
println(first(v), " ", last(v, 2))
try
v[0]
catch e
println(sprint(showerror, e))
end5 25 20
[10, 15, 20]
[5, 15, 25]
[15, 20, 25]
5 [20, 25]
BoundsError: attempt to access 5-element Vector{Int64} at index [0]
Modifying vectors
| Function | Effect |
|---|---|
push!(v, x) / pushfirst!(v, x) |
add at the end / start |
pop!(v) / popfirst!(v) |
remove and return the last / first item |
insert!(v, i, x) |
insert x at index i |
deleteat!(v, i) |
remove the item(s) at index i |
append!(v, w) |
add all items of w |
empty!(v) |
remove everything |
queue = ["ann"]
push!(queue, "bob", "cy")
pushfirst!(queue, "zoe")
println(queue)
println(pop!(queue), " ", popfirst!(queue))
insert!(queue, 2, "dan")
append!(queue, ["eve", "fay"])
println(queue)
deleteat!(queue, 1)
queue[1] = "DAN"
println(queue, " length ", length(queue))["zoe", "ann", "bob", "cy"]
cy zoe
["ann", "dan", "bob", "eve", "fay"]
["DAN", "bob", "eve", "fay"] length 4
Useful array functions
data = [4, 1, 7, 3, 7]
println(length(data), " ", sum(data), " ", maximum(data), " ", minimum(data))
println(sort(data), " ", sort(data, rev=true))
println(reverse(data), " ", unique(data))
println(findfirst(==(7), data), " ", findall(==(7), data))
println(argmax(data), " ", extrema(data))
println(7 in data, " ", count(isodd, data))
println(vcat([1, 2], [3]), " ", cumsum([1, 2, 3, 4]))5 22 7 1
[1, 3, 4, 7, 7] [7, 7, 4, 3, 1]
[7, 3, 7, 1, 4] [4, 1, 7, 3]
3 [3, 5]
3 (1, 7)
true 4
[1, 2, 3] [1, 3, 6, 10]
sort returns a new array, while sort! sorts in place — the same pattern as reverse/reverse! and unique/unique!.
Copying
Assignment never copies an array. Use copy for a shallow copy and deepcopy when the array contains other mutable objects:
a = [[1, 2], [3, 4]]
shallow = copy(a)
deep = deepcopy(a)
a[1][1] = 99
println(shallow)
println(deep)[[99, 2], [3, 4]]
[[1, 2], [3, 4]]
The shallow copy is a new outer array, but its elements are the same inner arrays as in a.
Test yourself
What is [3, 6, 9, 12][end-1]?
- 6
- 9
- 12
- an error
What is the type of [1, 2.0]?
- Vector{Any}
- Vector{Int64}
- Vector{Float64}
- Vector{Number}
Which function removes and returns the last element of a vector?
- pop!
- push!
- deleteat!
- last