Arrays in Crystal are an ordered collection of elements, generally of the same type. Although, it is possible to have an array of multiple types.

Creating an array

crystal

# Create an array of strings
stuff_and_things = ["stuff", "things"]

# Deduce the type signature of the array. 
typeof(stuff_and_things) # => Array(String)

# Alternate array syntax for strings
stuff_and_things = %w(stuff things)

# Alternate array syntax for symbols
stuff_and_things = %i(:stuff :things)

# To create an empty array, you must define 
# the intended type of its elements.
# There is 2 ways to define an empty array.
stuff_and_things = [] of String
stuff_and_things = Array(String).new
    

Iterating an Array

crystal

# Iterate through an array
stuff_and_things.each do |item|
  puts item
end

# Iterate through an array with an index
stuff_and_things.each_with_index do |item, idx|
  puts "#{idx} #{item}"
end
    

Accessing Elements

crystal

# Get the first element
stuff_and_things[0]

# Get the last element
stuff_and_things[-1]

# Get a sub array of elements
stuff_and_things[1..2]

# Check if array index exists
stuff_and_things[10]?

# Check for element existance
stuff_and_things.includes?("stuff")
    

Array Operations

crystal

# The shovel (<<) operator can be used to add 
# elements to the end of an array
stuff_and_things << "blah"

# Delete an element
stuff_and_things.delete("stuff")

# Remove the first element
stuff_and_things.shift

# Remove the last element
stuff_and_things.pop

# Get the size of an array
stuff_and_things.size # => 3
    

Static Arrays

If the size of an array is fixed it is much more performant to create a static array.

crystal

# Create a static array
stuff_and_things = StaticArray["stuff", "things"]

# Check the type of a static array
typeof(stuff_and_things) # => StaticArray(String, 2)
    

Considerations

  • Attempting to access an element of an array that is out of bounds is a runtime error.

Tags

crystal