Multidimensional Arrays

Other topics

Initializing a 2D array

Let's first recap how to initialize a 1D ruby array of integers:

my_array = [1, 1, 2, 3, 5, 8, 13]

Being a 2D array simply an array of arrays, you can initialize it like this:

my_array = [
  [1, 1, 2, 3, 5, 8, 13],
  [1, 4, 9, 16, 25, 36, 49, 64, 81],
  [2, 3, 5, 7, 11, 13, 17]
]

Initializing a 3D array

You can go a level further down and add a third layer of arrays. The rules don't change:

my_array = [
  [
    [1, 1, 2, 3, 5, 8, 13],
    [1, 4, 9, 16, 25, 36, 49, 64, 81],
    [2, 3, 5, 7, 11, 13, 17]
  ],
  [
    ['a', 'b', 'c', 'd', 'e'],
    ['z', 'y', 'x', 'w', 'v']
  ],
  [
    []
  ]
]

Accessing a nested array

Accessing the 3rd element of the first subarray:

my_array[1][2]

Array flattening

Given a multidimensional array:

my_array = [[1, 2], ['a', 'b']]

the operation of flattening is to decompose all array children into the root array:

my_array.flatten

# [1, 2, 'a', 'b']

Contributors

Topic Id: 10608

Example Ids: 31832,31833,31834,31835

This site is not affiliated with any of the contributors.