Multidimensional array with different lengths

Yes and no. First the no:

Proper arrays in Fortran, such as those declared like this:

integer, dimension(3,3,4) :: an_array

or like this

integer, dimension(:,:,:,:), allocatable :: an_array

are regular; for each dimension there is only one extent.

But, if you want to define your own type for a ragged array you can, and it’s relatively easy:

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

With this sort of approach you can allocate the elements of each of the vectors to a different size. For example:

type(ragged_array) :: ragarr
...
allocate(ragarr%vectors(5))
...
allocate(ragarr%vectors(1)%elements(3))
allocate(ragarr%vectors(2)%elements(4))
allocate(ragarr%vectors(3)%elements(6))

Leave a Comment