数组构造函数
数组构造函数是指定标量值的序列。 它构造一个秩为一的数组,其元素值是在序列中指定的值。 可以使用内部函数构造秩大于 1 的数组。 请参阅 RESHAPE (SOURCE , SHAPE , PAD , ORDER) 以获取详细信息。
语法
- i_d_type_spec
是 intrinsic_type_spec 或 derived_type_spec。 有关可能的类型规范的列表,请参阅 类型声明。
不能将 BYTE 用作数组构造函数中的 intrinsic_type_spec 。
- ac_value
- 是用于为数组元素提供值的表达式或隐式-DO 列表。
规则
i_d_type_spec 指定数组构造函数的类型和类型参数。 每个 ac_value 表达式必须与具有类型和类型参数的变量的内部赋值兼容。 每个值都将转换为数组构造函数的类型参数。
- 如果指定内在类型,则数组构造函数中的每个 ac_value 表达式必须是与您指定的类型兼容的内部类型。
- 如果您指定派生类型,则数组构造函数中的所有 ac_value 表达式都必须属于该派生类型,并且具有与您指定的类型相同的种类类型参数值。
如果省略 i_d_type_spec,那么数组构造函数中的每个 ac_value 表达式必须具有相同的类型和类型参数。
如果出现的 i_d_type_spec 不含 ac_value,那么将创建一个大小为零的一秩数组。
ac_value 符合以下规则:
- 如果它是标量表达式,那么它的值指定数组构造函数的一个元素。
- 如果是数组表达式,则表达式元素的值,按照数组元素顺序,指定数组构造函数对应的元素顺序。
- 如果它是隐式DO 列表,那么将展开它以在 implied_do_variable的控制下形成 ac_value 序列,如 DO 构造中所示。

如果 ac_value 是多态实体,那么将使用其声明的类型。 因为无限多态实体没有声明的类型,所以不能将其用于 ac_value。
如果您使用 -qxlf2003=dynamicacval 编译程序, ac_value的动态类型将被使用,并且您可以将无限多态实体用于 ac_value。
有关使用多态实体 的ac_value 的更多详情,请参见示例2。
有关无限多态实体以及这些实体的声明类型和动态类型的更多信息,请参阅多态实体。

示例
示例 1:不同数组构造函数
INTEGER, DIMENSION(5) :: a, b, c, d(2, 2)
CHARACTER(5), DIMENSION(3) :: color
! Assign values to all elements in a
a = (/1, 2, 3, 4, 5/)
! Assign values to some elements
a(3:5) = (/0, 1, 0/)
! Construct temporary logical mask
c = MERGE(a, b, (/T, F, T, T, F/))
! The array constructor produces a rank-one array, which
! is turned into a 2x2 array that can be assigned to d.
d = RESHAPE(SOURCE = (/1, 2, 1, 2/), SHAPE = (/2, 2/))
! Here, the constructor linearizes the elements of d in
! array-element order into a one-dimensional result.
PRINT *, a((/d/))
! Without a type_spec,each character literal must be of length 5
color = ['RED ', 'GREEN', 'BLUE ']
! With a type_spec, padding and truncation of each character literal occurs
color = [CHARACTER(5) :: 'RED', 'GREEN', 'BLUE']
示例 2:多态实体作为 ac_value
PROGRAM PolyAcValues
TYPE base
INTEGER :: i
END TYPE
TYPE, EXTENDS(base) :: child
INTEGER :: j
END TYPE
TYPE(base) :: baseType = base(3)
TYPE(child) :: childType = child(4, 6)
! Declare a polymorphic entity of base type
CLASS(base), ALLOCATABLE :: baseClass
! Declare an unlimited polymorphic entity. It has no declared type.
! Its dynamic type can be any derived type or intrinsic type
CLASS(*), ALLOCATABLE :: anyClass
! Declare a deferred-shape array of unlimited polymorphic entities
CLASS(*), ALLOCATABLE :: anyClassArr(:)
! Allocate a child item to baseClass. The dynamic type of bassClass is child.
ALLOCATE(baseClass, source = childType)
! Polymorphic entities used in the array constructor
ALLOCATE(anyClassArr(2), source = [baseClass, baseClass])
! Because the compiler uses the declared type, which is base, and the result
! is "Base item: 4 4". If you specify -qxlf2003=dynamicacval, the compiler uses
! the dynamic type, which is child, and the result is "Child item: 4,6 4,6".
CALL printAny(anyClassArr, 2)
DEALLOCATE(anyClassArr)
DEALLOCATE(baseClass)
! Allocate a base item to anyClass. The dynamic type of anyClass is base.
ALLOCATE(anyClass, source = baseType)
! Unlimited polymorphic entities used in the array constructor
ALLOCATE(anyClassArr(2), source = [anyClass, anyClass])
! If you specify -qxlf2003=dynamicacval, the use of unlimited polymorphic
! entities in the array constructor is valid, and the compiler uses the
! dynamic type, which is base. The result is "Base item: 3 3"; Otherwise,
! a severe error is issued at compile time.
CALL printAny(anyClassArr, 2)
DEALLOCATE(anyClassArr)
DEALLOCATE(anyClass)
CONTAINS
SUBROUTINE printAny(printItem, len)
CLASS(*) :: printItem(len)
DO i = 1, len
SELECT TYPE (item => printItem(i))
TYPE IS (base)
PRINT *, 'Base item: ', item
TYPE IS (child)
PRINT *, 'Child item: ', item
END SELECT
END DO
END SUBROUTINE
END PROGRAM
