ZetCode

Julia Arrays Tutorial

last modified November 18, 2026

Arrays in Julia are collections of elements, all of the same type, stored in a contiguous block of memory. They are one of the most versatile and commonly used data structures in Julia. This tutorial covers basic and advanced usage of arrays with practical examples.

Arrays can be one-dimensional (vectors), two-dimensional (matrices), or multi-dimensional. They are mutable, meaning their contents can be modified after creation. The examples were verified with Julia 1.13.4.

Creating Arrays

This example demonstrates how to create a simple one-dimensional array.

main.jl
arr = [1, 2, 3, 4, 5]
println(arr)

The array arr contains the integers 1 through 5. Square brackets with comma-separated values create a vector; every element has the same type, here Int.

$ julia main.jl
[1, 2, 3, 4, 5]

Accessing Array Elements

This example shows how to access elements in an array using indices.

main.jl
arr = [10, 20, 30, 40, 50]
println(arr[3])

Arrays in Julia are 1-indexed, so arr[3] accesses the third element, which is 30.

$ julia main.jl
30

Modifying Array Elements

This example demonstrates how to modify an element in an array.

main.jl
arr = [1, 2, 3, 4, 5]
arr[2] = 99
println(arr)

The second element of arr is changed to 99. Assignment through an index works because arrays are mutable.

$ julia main.jl
[1, 99, 3, 4, 5]

Creating a 2D Array

This example shows how to create a two-dimensional array (matrix).

main.jl
matrix = [1 2 3; 4 5 6; 7 8 9]
println(matrix)

The matrix is a 3x3 array with elements arranged in rows and columns. Spaces separate values within a row and semicolons separate the rows.

$ julia main.jl
[1 2 3; 4 5 6; 7 8 9]

Array Slicing

This example demonstrates how to slice an array to extract a subset of elements.

main.jl
arr = [10, 20, 30, 40, 50]
slice = arr[2:4]
println(slice)

The slice arr[2:4] extracts elements from index 2 to 4. The range 2:4 is inclusive of both endpoints.

$ julia main.jl
[20, 30, 40]

Concatenating Arrays

This example shows how to concatenate two arrays.

main.jl
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
combined = [arr1; arr2]
println(combined)

The ; operator concatenates arr1 and arr2 into a single vector, placing the elements of the second array after those of the first.

$ julia main.jl
[1, 2, 3, 4, 5, 6]

Array Comprehensions

This example demonstrates how to create an array using a comprehension.

main.jl
squares = [x^2 for x in 1:5]
println(squares)

The comprehension generates an array of squares for numbers 1 through 5. It is a compact way to build an array from an iterator.

$ julia main.jl
[1, 4, 9, 16, 25]

Finding Array Length

This example shows how to find the length of an array.

main.jl
arr = [1, 2, 3, 4, 5]
println(length(arr))

The length function returns the number of elements in arr.

$ julia main.jl
5

Sorting Arrays

This example demonstrates how to sort an array.

main.jl
arr = [5, 3, 1, 4, 2]
sorted_arr = sort(arr)
println(sorted_arr)

The sort function returns a new array with the elements of arr in ascending order; the original array is not modified.

$ julia main.jl
[1, 2, 3, 4, 5]

Multidimensional Array Operations

This example shows how to perform operations on a multidimensional array.

main.jl
matrix = [1 2; 3 4]
sum_matrix = sum(matrix, dims=1)
println(sum_matrix)

The sum function with dims=1 calculates the sum of each column in matrix, returning a 1x2 array.

$ julia main.jl
[4 6]

Best Practices for Arrays

A few guidelines help keep array code efficient and easy to follow.

Source

Julia Arrays documentation

Julia Arrays manual

In this article we have worked with arrays in Julia: creation, indexing, slicing, concatenation, comprehensions, sorting, and operations over dimensions. Tuples, which are immutable, are covered in Julia tuples, and element-wise operations in Julia operators.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.