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:

heap/naive.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.merge(*iterables, heap : Array(Tuple(T, Int32, Int32)) | Nil = nil, &block : T -> _) #

[View source]
def self.merge_by(*iterables, key_func : T -> K, heap : Array(Tuple(K, T, Int32, Int32)) | Nil = nil, &block : T -> _) forall K #

[View source]

Instance Method Detail

def _arg_nlargest_by(n, heap : Array(Tuple(K, Int32)) | Nil = nil, &key_func : T -> K) forall K #

[View source]
def _arg_nsmallest_by(n, heap : Array(Tuple(K, Int32)) | Nil = nil, &key_func : T -> K) forall K #

[View source]
def arg_nlargest(n, heap : Array(Tuple(T, Int32)) | Nil = nil) #

[View source]
def arg_nlargest_by(n, heap : Array(Tuple(K, Int32)) | Nil = nil, &key_func : T -> K) forall K #

[View source]
def arg_nsmallest(n, heap : Array(Tuple(T, Int32)) | Nil = nil) #

[View source]
def arg_nsmallest_by(n, heap : Array(Tuple(K, Int32)) | Nil = nil, &key_func : T -> K) forall K #

[View source]
def heap_pop #

[View source]
def heap_pop_max #

[View source]
def heap_push(item) #

[View source]
def heap_push_max(item) #

[View source]
def heap_pushpop(item) #

[View source]
def heap_pushpop_max(item) #

[View source]
def heap_replace(item) #

[View source]
def heap_replace_max(item) #

[View source]
def heapify #

[View source]
def heapify! #

[View source]
def heapify_max #

[View source]
def heapify_max! #

[View source]
def nlargest(n, heap : Array(T) | Nil = nil) #

[View source]
def nlargest_by(n, heap : Array(Tuple(K, T)) | Nil = nil, &key_func : T -> K) forall K #

[View source]
def nsmallest(n, heap : Array(T) | Nil = nil) #

[View source]
def nsmallest_by(n, heap : Array(Tuple(K, T)) | Nil = nil, &key_func : T -> K) forall K #

[View source]