class Array(T)

Overview

An Array is an ordered, integer-indexed collection of objects of type T.

Array indexing starts at 0. A negative index is assumed to be relative to the end of the array: -1 indicates the last element, -2 is the next to last element, and so on.

An Array can be created using the usual new method (several are provided), or with an array literal:

Array(Int32).new  # => []
[1, 2, 3]         # Array(Int32)
[1, "hello", 'x'] # Array(Int32 | String | Char)

See Array literals in the language reference.

An Array can have mixed types, meaning T will be a union of types, but these are determined when the array is created, either by specifying T or by using an array literal. In the latter case, T will be set to the union of the array literal elements' types.

When creating an empty array you must always specify T:

[] of Int32 # same as Array(Int32)
[]          # syntax error

An Array is implemented using an internal buffer of some capacity and is reallocated when elements are pushed to it when more capacity is needed. This is normally known as a dynamic array.

You can use a special array literal syntax with other types too, as long as they define an argless new method and a << method. Set is one such type:

set = Set{1, 2, 3} # => Set{1, 2, 3}
set.class          # => Set(Int32)

The above is the same as this:

set = Set(typeof(1, 2, 3)).new
set << 1
set << 2
set << 3

Included Modules

Defined in:

array/as_tuple.cr
array/requiring.cr
array/split_by.cr
array/when_empty.cr

Instance Method Summary

Instance methods inherited from class Object

methods : Array(String) methods, pipe(&) pipe, selftap(&) selftap

Instance Method Detail

def as_10_tuple! #

[View source]
def as_10_tuple? #

[View source]
def as_11_tuple! #

[View source]
def as_11_tuple? #

[View source]
def as_12_tuple! #

[View source]
def as_12_tuple? #

[View source]
def as_13_tuple! #

[View source]
def as_13_tuple? #

[View source]
def as_14_tuple! #

[View source]
def as_14_tuple? #

[View source]
def as_15_tuple! #

[View source]
def as_15_tuple? #

[View source]
def as_16_tuple! #

[View source]
def as_16_tuple? #

[View source]
def as_17_tuple! #

[View source]
def as_17_tuple? #

[View source]
def as_18_tuple! #

[View source]
def as_18_tuple? #

[View source]
def as_19_tuple! #

[View source]
def as_19_tuple? #

[View source]
def as_1_tuple! #

[View source]
def as_1_tuple? #

[View source]
def as_20_tuple! #

[View source]
def as_20_tuple? #

[View source]
def as_2_tuple! #

[View source]
def as_2_tuple? #

[View source]
def as_3_tuple! #

[View source]
def as_3_tuple? #

[View source]
def as_4_tuple! #

[View source]
def as_4_tuple? #

[View source]
def as_5_tuple! #

[View source]
def as_5_tuple? #

[View source]
def as_6_tuple! #

[View source]
def as_6_tuple? #

[View source]
def as_7_tuple! #

[View source]
def as_7_tuple? #

[View source]
def as_8_tuple! #

[View source]
def as_8_tuple? #

[View source]
def as_9_tuple! #

[View source]
def as_9_tuple? #

[View source]
def requiring(minimum : Int32, &) #

[View source]
def split_by(separator : T) : Array(Array(T)) #

[View source]
def when_empty(&) #

[View source]
def when_not_empty(&) #

[View source]