Arrays are a fundamental way to store and manage ordered lists of items in Julia. Think of an array as a sequence of numbered containers, where each container can hold a piece of data, and you can access or change the contents of any container if you know its number. Because arrays keep items in a specific order and allow you to change their contents, they are incredibly versatile for a wide range of programming tasks, from storing a list of student names to managing pixel data in an image. A one-dimensional array in Julia is often called a Vector.
There are several ways to create arrays in Julia. The most straightforward method is to list the elements you want in the array, separated by commas, and enclose them in square brackets [].
# An array of integers
scores = [90, 85, 92, 78, 95]
# An array of strings
names = ["Alice", "Bob", "Charlie"]
# An array can also hold mixed data types
mixed_data = [10, "Julia", 3.14, true]
When you create an array with mixed data types like mixed_data above, Julia makes it an array of type Any, which means it can hold values of any type. While flexible, it's often more efficient and clearer if your arrays store elements of the same type. You can specify the type of an array when you create it:
# An array specifically for 64-bit floating-point numbers
temperatures = Float64[23.5, 24.1, 22.8]
# An array specifically for integers
quantities = Int[10, 20, 5]
You can also create an empty array, which you might want to fill with data later:
# An empty array that can hold any type (Vector{Any})
empty_list = []
# An empty array intended to hold strings
empty_strings = String[]
Julia also provides functions to create arrays with initial placeholder values:
zeros(T, n) creates an array of length n filled with zeros of type T. If T is omitted, it defaults to Float64.ones(T, n) creates an array of length n filled with ones of type T. If T is omitted, it defaults to Float64.fill(value, n) creates an array of length n filled with value.initial_zeros = zeros(3) # [0.0, 0.0, 0.0]
initial_ones_int = ones(Int, 4) # [1, 1, 1, 1]
default_values = fill("pending", 2) # ["pending", "pending"]
To work with elements in an array, you need to know their position, or index. An important characteristic of Julia (and some other scientific computing languages) is that arrays are 1-based indexed. This means the first element is at index 1, the second element is at index 2, and so on. This might be different if you've used languages like Python or C++, which use 0-based indexing.
Consider our scores array: [90, 85, 92, 78, 95]
90 is at index 1.85 is at index 2.95 is at index 5.The
scoresarray shown with its 1-based indices and corresponding values.
You can retrieve an element from an array by specifying its index in square brackets after the array's name.
scores = [90, 85, 92, 78, 95]
first_score = scores[1] # Accesses the first element: 90
third_score = scores[3] # Accesses the third element: 92
println("The first score is: ", first_score)
println("The third score is: ", third_score)
Julia provides a handy keyword end to refer to the last index of an array. This is useful when you don't know the exact length of the array or want to access elements near the end.
last_score = scores[end] # Accesses the last element: 95
second_to_last = scores[end-1] # Accesses the second to last element: 78
println("Last score: ", last_score)
println("Second to last score: ", second_to_last)
If you try to access an index that is outside the valid range of the array (e.g., index 0, or an index greater than the array's length), Julia will raise a BoundsError.
Slicing Arrays
You can also extract a portion of an array, known as a slice. Slicing creates a new array containing the specified range of elements. You define a slice using the start_index:end_index syntax.
scores = [90, 85, 92, 78, 95, 88, 70]
# Get elements from index 2 up to index 4
middle_scores = scores[2:4] # Result: [85, 92, 78]
println("Middle scores: ", middle_scores)
# Get elements from index 3 to the end
scores_from_third = scores[3:end] # Result: [92, 78, 95, 88, 70]
println("Scores from third: ", scores_from_third)
# Get the first three elements
first_three = scores[1:3] # Result: [90, 85, 92]
println("First three scores: ", first_three)
It's important to remember that a slice creates a copy of that part of the array, not a view into the original array (by default). Modifying a slice will not affect the original array.
Arrays in Julia are mutable, which means you can change their content after they are created. You can change individual elements, add new ones, or remove existing ones.
Changing an Element To change an element at a specific index, you assign a new value to it:
tasks = ["Write report", "Send emails", "Meeting at 3 PM"]
println("Original tasks: ", tasks)
# Update the first task
tasks[1] = "Finalize report"
println("Updated tasks: ", tasks) # Output: ["Finalize report", "Send emails", "Meeting at 3 PM"]
# Change the last task
tasks[end] = "Team meeting at 3 PM"
println("Further updated tasks: ", tasks) # Output: ["Finalize report", "Send emails", "Team meeting at 3 PM"]
Adding Elements
Julia provides several functions to add elements to an array. Many functions that modify their arguments in Julia have names ending with an exclamation mark (!). This is a convention to alert you that the function will change one of its inputs directly.
push!(array, item): Adds item to the end of array.append!(array, collection): Adds all elements from collection to the end of array.numbers = [10, 20, 30]
println("Initial numbers: ", numbers)
push!(numbers, 40)
println("After push!: ", numbers) # Output: [10, 20, 30, 40]
new_items = [50, 60]
append!(numbers, new_items)
println("After append!: ", numbers) # Output: [10, 20, 30, 40, 50, 60]
Removing Elements Similarly, there are functions to remove elements:
pop!(array): Removes the last element from array and returns it.popfirst!(array): Removes the first element from array and returns it.splice!(array, index_or_range, [replacement_values...]): Removes element(s) at index_or_range and optionally inserts replacement_values. If no replacements are given, it just removes.items = ["A", "B", "C", "D", "E"]
println("Original items: ", items)
last_item = pop!(items)
println("Removed last item: ", last_item) # Output: "E"
println("Items after pop!: ", items) # Output: ["A", "B", "C", "D"]
first_item = popfirst!(items)
println("Removed first item: ", first_item) # Output: "A"
println("Items after popfirst!: ", items) # Output: ["B", "C", "D"]
# Remove the element at index 2 ("C")
removed_middle = splice!(items, 2)
println("Removed middle item: ", removed_middle) # Output: "C" (as a single-element array in some contexts, or just the element)
println("Items after splice!: ", items) # Output: ["B", "D"]
Note: splice! returns the removed element(s) as an array.
Julia offers many built-in functions to work with arrays effectively:
length(array): Returns the number of elements in the array.
my_list = [5, 10, 15, 20]
println("Length of my_list: ", length(my_list)) # Output: 4
isempty(array): Returns true if the array has no elements, false otherwise.
empty_arr = []
println("Is empty_arr empty? ", isempty(empty_arr)) # Output: true
println("Is my_list empty? ", isempty(my_list)) # Output: false
eltype(array): Returns the type of the elements the array is designated to hold.
int_array = [1, 2, 3]
println("Element type of int_array: ", eltype(int_array)) # Output: Int64 (or Int32 depending on system)
any_array = [1, "hello", 3.0]
println("Element type of any_array: ", eltype(any_array)) # Output: Any
size(array): Returns a tuple containing the dimensions of the array. For a vector (1D array), it's a 1-tuple (length,).
vector_example = [10, 20, 30, 40]
println("Size of vector_example: ", size(vector_example)) # Output: (4,)
in(item, array) or item ∈ array: Checks if item is present in array.
fruits = ["apple", "banana", "cherry"]
println("Is 'banana' in fruits? ", "banana" in fruits) # Output: true
println("Is 'orange' in fruits? ", "orange" ∈ fruits) # Output: false (you can type ∈ as \in TAB)
As mentioned earlier, Julia arrays are typed. When you create [1, 2, 3], Julia infers it as Vector{Int} (or Vector{Int64} more specifically on most systems). If you create [1.0, 2.5, 3.1], it becomes Vector{Float64}. If you mix types like [1, "two", 3.0], Julia uses Vector{Any}.
Using type-specific arrays (like Vector{Int}) is generally better for performance than Vector{Any} because Julia can make more optimizations when it knows exactly what kind of data an array holds. However, Vector{Any} provides flexibility when you genuinely need to store different types of items in the same collection.
Arrays are a foundation of data manipulation in Julia. Their ordered and mutable nature makes them suitable for many tasks. As you progress, you'll encounter multi-dimensional arrays (matrices, etc.) and more advanced array operations, but this foundation in creating, accessing, and modifying one-dimensional arrays (vectors) will serve you well.
Was this section helpful?
Vector{Int} vs Vector{Any}).© 2026 ApX Machine LearningAI Ethics & Transparency•