Julia Arrays
Arrays are one of the most important data structures in Julia, used for storing ordered collections of elements. Julia's arrays are powerful, supporting multi-dimensional arrays and rich operations.
Creating Arrays
Literal Creation
julia
# One-dimensional array (vector)
arr = [1, 2, 3, 4, 5]
println(arr) # [1, 2, 3, 4, 5]
# Specify type
arr_float = Float64[1.0, 2.0, 3.0]
# Empty array
empty_arr = Int[]
empty_arr2 = [] # Any typeUsing Ranges
julia
# Using range
arr = collect(1:5) # [1, 2, 3, 4, 5]
arr = collect(1:2:10) # [1, 3, 5, 7, 9], step of 2
arr = collect(10:-1:1) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# Using range function
arr = collect(range(0, 1, length=5)) # [0.0, 0.25, 0.5, 0.75, 1.0]Using Constructors
julia
# All zeros array
zeros(5) # 5 zeros (Float64)
zeros(Int, 5) # 5 zeros (Int)
zeros(3, 4) # 3×4 zero matrix
# All ones array
ones(5) # 5 ones (Float64)
ones(Int, 3) # 3 ones (Int)
# Uninitialized array (better performance, random values)
arr = Vector{Int}(undef, 5)
mat = Matrix{Float64}(undef, 3, 4)
# Fill with specific value
fill(7, 5) # [7, 7, 7, 7, 7]
fill("a", 3) # ["a", "a", "a"]Using Comprehensions
julia
# Array comprehension
squares = [x^2 for x in 1:5] # [1, 4, 9, 16, 25]
evens = [x for x in 1:10 if x % 2 == 0] # [2, 4, 6, 8, 10]
# Multi-dimensional comprehension
matrix = [i + j for i in 1:3, j in 1:4] # 3×4 matrixArray Indexing
Julia arrays are 1-indexed (not 0).
Basic Indexing
julia
arr = [10, 20, 30, 40, 50]
# Access single element
println(arr[1]) # 10 (first element)
println(arr[end]) # 50 (last element)
println(arr[end-1]) # 40 (second to last)
# Modify element
arr[1] = 100
println(arr) # [100, 20, 30, 40, 50]Range Indexing (Slicing)
julia
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Get subarray
println(arr[2:5]) # [2, 3, 4, 5]
println(arr[1:2:end]) # [1, 3, 5, 7, 9], step of 2
println(arr[end:-1:1]) # Reverse array
# From start or to end
println(arr[1:3]) # [1, 2, 3]
println(arr[8:end]) # [8, 9, 10]Multi-dimensional Indexing
julia
# Create 3×4 matrix
matrix = [i + j*10 for i in 1:3, j in 1:4]
# 3×4 Matrix{Int64}:
# 11 21 31 41
# 12 22 32 42
# 13 23 33 43
# Access single element
println(matrix[2, 3]) # 32 (row 2, column 3)
# Access entire row or column
println(matrix[1, :]) # Row 1: [11, 21, 31, 41]
println(matrix[:, 2]) # Column 2: [21, 22, 23]
# Submatrix
println(matrix[1:2, 2:3]) # 2×2 submatrixArray Operations
Adding Elements
julia
arr = [1, 2, 3]
# Add to end
push!(arr, 4)
println(arr) # [1, 2, 3, 4]
# Add to beginning
pushfirst!(arr, 0)
println(arr) # [0, 1, 2, 3, 4]
# Insert at position
insert!(arr, 3, 100)
println(arr) # [0, 1, 100, 2, 3, 4]
# Append another array
append!(arr, [5, 6])
println(arr) # [0, 1, 100, 2, 3, 4, 5, 6]Removing Elements
julia
arr = [1, 2, 3, 4, 5]
# Remove from end
last = pop!(arr)
println(last) # 5
println(arr) # [1, 2, 3, 4]
# Remove from beginning
first = popfirst!(arr)
println(first) # 1
println(arr) # [2, 3, 4]
# Remove at position
deleted = deleteat!(arr, 2)
println(arr) # [2, 4]
# Remove specific value
arr = [1, 2, 3, 2, 4]
filter!(x -> x != 2, arr)
println(arr) # [1, 3, 4]Merging Arrays
julia
a = [1, 2, 3]
b = [4, 5, 6]
# Vertical concatenation (creates new array)
c = vcat(a, b) # [1, 2, 3, 4, 5, 6]
c = [a; b] # same
# For matrices
m1 = [1 2; 3 4]
m2 = [5 6; 7 8]
m = vcat(m1, m2) # 4×2 matrix
# Horizontal concatenation
m = hcat(m1, m2) # 2×4 matrix
m = [m1 m2] # sameCopying Arrays
julia
arr = [1, 2, 3]
# Shallow copy
arr_copy = copy(arr)
# Deep copy (for nested arrays)
nested = [[1, 2], [3, 4]]
nested_copy = deepcopy(nested)Array Properties
julia
arr = [1, 2, 3, 4, 5]
matrix = [1 2 3; 4 5 6] # 2×3 matrix
# Length
println(length(arr)) # 5
println(length(matrix)) # 6 (total elements)
# Dimensions
println(ndims(arr)) # 1
println(ndims(matrix)) # 2
# Size
println(size(arr)) # (5,)
println(size(matrix)) # (2, 3)
println(size(matrix, 1)) # 2 (rows)
println(size(matrix, 2)) # 3 (columns)
# Element type
println(eltype(arr)) # Int64
# Is empty
println(isempty(arr)) # false
println(isempty([])) # trueArray Computation
Basic Statistics
julia
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
println(sum(arr)) # 55 (sum)
println(prod(arr)) # 3628800 (product)
println(minimum(arr)) # 1 (min)
println(maximum(arr)) # 10 (max)
println(extrema(arr)) # (1, 10) (min and max)
using Statistics
println(mean(arr)) # 5.5 (average)
println(median(arr)) # 5.5 (median)
println(std(arr)) # 3.0277... (standard deviation)
println(var(arr)) # 9.166... (variance)Finding Positions
julia
arr = [10, 20, 30, 40, 50]
# Find min/max positions
println(argmin(arr)) # 1 (position of min)
println(argmax(arr)) # 5 (position of max)
# Find specific value
println(findfirst(==(30), arr)) # 3
println(findall(x -> x > 25, arr)) # [3, 4, 5]
# Check if element exists
println(30 in arr) # true
println(in(30, arr)) # trueSorting
julia
arr = [3, 1, 4, 1, 5, 9, 2, 6]
# Return sorted new array
sorted = sort(arr)
println(sorted) # [1, 1, 2, 3, 4, 5, 6, 9]
# In-place sort
sort!(arr)
# Descending sort
sorted_desc = sort(arr, rev=true)
# Custom sort rule
words = ["banana", "apple", "cherry"]
sort(words, by=length) # Sort by length
# Get sort indices
arr = [30, 10, 20]
println(sortperm(arr)) # [2, 3, 1]Reversing
julia
arr = [1, 2, 3, 4, 5]
# Return reversed new array
reversed = reverse(arr)
println(reversed) # [5, 4, 3, 2, 1]
# In-place reverse
reverse!(arr)Vectorized Operations
Broadcasting (Dot Operations)
Use . for element-wise operations:
julia
arr = [1, 2, 3, 4, 5]
# Element-wise operations
println(arr .+ 10) # [11, 12, 13, 14, 15]
println(arr .* 2) # [2, 4, 6, 8, 10]
println(arr .^ 2) # [1, 4, 9, 16, 25]
# Two array operations
a = [1, 2, 3]
b = [4, 5, 6]
println(a .+ b) # [5, 7, 9]
println(a .* b) # [4, 10, 18]
# Function broadcasting
println(sqrt.(arr)) # [1.0, 1.414..., 1.732..., 2.0, 2.236...]map Function
julia
arr = [1, 2, 3, 4, 5]
# Apply function to each element
result = map(x -> x^2, arr)
println(result) # [1, 4, 9, 16, 25]
# Multiple arrays
a = [1, 2, 3]
b = [4, 5, 6]
result = map((x, y) -> x + y, a, b)
println(result) # [5, 7, 9]filter Function
julia
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Filter elements
evens = filter(x -> x % 2 == 0, arr)
println(evens) # [2, 4, 6, 8, 10]
# In-place filter
arr2 = [1, 2, 3, 4, 5]
filter!(x -> x > 2, arr2)
println(arr2) # [3, 4, 5]reduce Function
julia
arr = [1, 2, 3, 4, 5]
# Cumulative computation
sum_result = reduce(+, arr)
println(sum_result) # 15
product = reduce(*, arr)
println(product) # 120
# With initial value
result = reduce(+, arr, init=100)
println(result) # 115Multi-dimensional Arrays
Creating Multi-dimensional Arrays
julia
# 2D array (matrix)
matrix = [1 2 3; 4 5 6; 7 8 9]
# 3×3 Matrix{Int64}:
# 1 2 3
# 4 5 6
# 7 8 9
# 3D array
arr3d = zeros(2, 3, 4) # 2×3×4 array
# reshape
arr = collect(1:12)
matrix = reshape(arr, 3, 4) # 3×4 matrixMatrix Operations
julia
A = [1 2; 3 4]
B = [5 6; 7 8]
# Matrix addition
C = A + B
# Matrix multiplication
C = A * B # Matrix multiplication
C = A .* B # Element-wise multiplication
# Transpose
At = A' # or transpose(A)
# Inverse
using LinearAlgebra
Ainv = inv(A)
# Determinant
det(A)
# Eigenvalues
eigvals(A)Common Techniques
Pre-allocate Arrays
julia
# Inefficient: dynamic growth
result = []
for i in 1:1000
push!(result, i^2)
end
# Efficient: pre-allocate
result = Vector{Int}(undef, 1000)
for i in 1:1000
result[i] = i^2
end
# Or use comprehension
result = [i^2 for i in 1:1000]Views (Avoid Copying)
julia
arr = [1, 2, 3, 4, 5]
# Slicing creates copy
slice = arr[2:4] # New array
# view creates view (no copy)
v = view(arr, 2:4) # or @view arr[2:4]
v[1] = 100
println(arr) # [1, 100, 3, 4, 5], original modifiedNext Steps
After learning arrays, continue with:
- Tuples - Immutable ordered collections
- Dictionaries and Sets - Other data structures
- Math Functions - Mathematical operations