2017-06-06 45 views
0

我看到this post關於製作一個破舊的數組。語法訪問一個衣衫襤褸的數組?

當我試圖做一切工作,直到我想訪問這個數組。

type :: vector 
    integer, dimension(:), allocatable :: elements 
end type vector 
type :: ragged_array 
    type(vector), dimension(:), allocatable :: vectors 
end type ragged_array 
type(ragged_array) :: ragarr 
allocate(ragarr%vectors(1)%elements(3)) 
allocate(ragarr%vectors(2)%elements(4)) 
!PROBLEM HERE : 
raggar(1,1:3)=0 
raggar(2,1:4)=1 

它給我的錯誤:

The assigment operation or the binary expression operation is invalid for the data type of two operands 

目前還不清楚,我如何操縱這個衣衫襤褸的數組,如何訪問一個特定的值...感謝您的幫助!

回答

2

你的代碼中包含了許多錯誤:

  1. 您應該分配raggar%載體之前分配其成分raggar%載體%的元素。
  2. raggar是含有包含分配數組分配數組標量,它是不是一個數組,如果要訪問它的元素可以只使用raggar%載體(ⅰ)%的元素(j)的

更正後的代碼:

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

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

type(ragged_array) :: ragarr 

allocate(raggar%vectors(2)) 
allocate(ragarr%vectors(1)%elements(3)) 
allocate(ragarr%vectors(2)%elements(4)) 

!PROBLEM HERE : 
raggar%vectors(1)%elements=0 !raggar(1,1:3)=0 
raggar%vectors(2)%elements=0 !raggar(2,1:4)=1 
+0

有沒有辦法讓整個'raggar'通過子程序的參數傳遞它?這似乎是做'raggar%矢量%元素'不起作用.... – Dadep

相關問題