HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JuliaVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Julia›Arrays

Arrays

Array creation, 1-based indexing, adding (push!), length and looping through.

Simple enough for a 5-year-old to understand

Create an array and access elements

Arrays in Julia are 1-indexed: the first element is a[1] (not a[0]!).

Example code
1
languages = ["Julia", "Go", "Rust"]
An array with three elements.
VariableAn array with three elements.
2
println(languages[1])
Julia (FIRST element, index 1!)
Output / InputJulia (FIRST element, index 1!)
3
println(length(languages))
3 (number of elements)
Output / Input3 (number of elements)
4
println(languages)
["Julia", "Go", "Rust"]
Output / Input["Julia", "Go", "Rust"]
VariableOutput / Input
💡 Hint: Most common mistake: Julia is 1-indexed, the first element is a[1].

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How to get the FIRST element in the array ["red", "blue"]?

About this topic

Arrays are Julia's most widely used data structure and are central to the language's design. One-dimensional vectors, two-dimensional matrices, and higher-dimensional arrays are simply different forms of the same type.

Indices start at one, and there is a special keyword for the last element. This allows you to access items from the end without calculating the array's length, and it is frequently used in slicing.

Arrays are reference types. When you assign an array to another variable, no copy is created; both names point to the same data. If you want an independent copy, you must explicitly copy it — this is a common source of silent bugs.

What you will learn

  • Creating arrays
  • One-based indexing
  • Slicing and the end keyword
  • Reference behavior
  • Bounds checking

Who is this for?

  • Researchers doing numerical computing
  • Developers accustomed to zero-based indexing
  • Those who get references while expecting copies

When to use?

Measurement series, matrices, and simulation results are stored in arrays; Julia's performance advantage is most evident here.

If you don't want an array's contents to change when passing it to a function, you must pass a copy.

Common mistakes

Wrong

dizi = [10, 20, 30]
println(dizi[0])

In Julia, indices start at one; there is no zero-th element and a BoundsError is thrown. The error is reported explicitly rather than silently returning a wrong value. This is the most common mistake made on day one by developers coming from other languages.

Correct

dizi = [10, 20, 30]
println(dizi[1])

Wrong

a = [1, 2, 3]
b = a
b[1] = 99
println(a[1])

Arrays are references; b and a point to the same data and 99 is printed to the screen. If you expected a copy, the result is surprising, and since it doesn't throw an error, it is hard to notice. For an independent copy, the copy function must be used.

Correct

a = [1, 2, 3]
b = copy(a)
b[1] = 99
println(a[1])

Wrong

dizi = [1, 2, 3]
for i in 1:length(dizi)
    if dizi[i] == 2
        deleteat!(dizi, i)
    end
end

Deleting elements while iterating changes the length of the array; in the next step, an index that no longer exists is accessed and a BoundsError is thrown. The correct way is to filter out what needs to be deleted to produce a new array or use a function that directly removes matching items.

Correct

dizi = [1, 2, 3]
dizi = filter(x -> x != 2, dizi)

Frequently asked questions

How do I access the last element?

You use the end keyword inside the index; there is no need to calculate the length. To access the second to last, you can subtract one from this keyword; it works the same way in slicing too.

Why are arrays stored in column-major order?

To follow the Fortran and MATLAB tradition. This means putting columns in the outer loop for nested loops is faster in terms of memory access; the difference becomes noticeable in large matrices.

Why am I getting a dimension mismatch error?

In broadcasting, the dimensions of the arrays must be compatible. Performing element-wise operations on two arrays of different lengths throws an error. Julia does not silently repeat; the compatibility rules are clear and reported when violated.

Summary

Indices start at one.

Out-of-bounds access throws an error.

Arrays are references; assignment does not produce a copy.

There is a special keyword for the last element.

Deleting elements while iterating causes an error.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code