liveBook · Manning

Array

As explained in Julia as a Second Language

An array is an ordered collection of elements that can have any Julia-supported element type and can be organized in one or more dimensions.

An array stores ordered values so you can work with a column of measurements or a whole table instead of handling each value separately. Its elements can be numbers, Booleans, characters, or strings. A one-dimensional array is a vector; a two-dimensional array is a matrix, much like a spreadsheet table. section 4.0 section 4.2

Start with a few practical operations:

  • Create a column vector with commas: `amounts = [4, 1, 5, 3, 2]`. Separate values with spaces for a one-row matrix, or separate rows with semicolons for a table. section 4.2
  • Read or replace an element with square brackets: `amounts[1]` is `4`. Julia normally starts indexing at 1; `begin` and `end` refer to the first and last positions. An invalid index raises a bounds error. section 4.5
  • For larger arrays, use `zeros`, `ones`, `fill`, or `rand`. Specify an element type when needed, and inspect it with `eltype`; `size` and `ndims` describe an array’s shape. section 4.6 section 14.2
  • Use `sum` to total elements and `length` to count them. Dotted operators such as `.*` request elementwise arithmetic. `sort` returns a new vector, while `sort!` changes its input. section 4.3

Calculate pizza revenue

Given `amounts = [4, 1, 5, 3, 2]` and `prices = [15.0, 11.5, 13.0, 12.75, 14.25]`, `amounts .* prices` computes revenue for each pizza type. `sum(amounts .* prices)` returns `203.25`. The dot matters: multiplying the two columns with `*` does not specify the intended elementwise operation. section 4.3

Arrays have one element type. A mixed literal such as `["hawaiian", 'S', 10.5]` can have element type `Any`; a tuple instead tracks each element’s type and cannot be changed. For parts of an array, range-based slices make copies; use `@view` when changes should refer to the original data. Arrays also support fast lookup by index, unlike linked lists, though insertion away from the end is not quick. section 4.9 section 14.7 section 12.8

How Julia as a Second Language covers it

How deep it goes
Works through construction, indexing, arithmetic, and shape queries, then adds slices and views; defers the details of element-type inference.
Examples
Uses pizza-sales quantities and prices, spreadsheet-style tables, and rocket-engine collections to demonstrate calculations, dimensions, and indexed access.
What it assumes
Builds from familiar calculator and spreadsheet operations, introducing vectors, matrices, and indexing rather than assuming linear algebra.
What you can do after
Apply it via Julia to create, inspect, index, transform, slice, and combine vectors and matrices.
How it connects
Connects arrays to vectors, matrices, tuples, elementwise operations, and linked lists.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.