衣衫array的数组中访问的语法? [英] syntax to access in a ragged array ?

查看:76
本文介绍了衣衫array的数组中访问的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章关于制作一个破烂的数组.

I saw this post about making a ragged array.

当我尝试执行此操作之前,一切都将正常进行,直到我要访问此数组为止.

when I try to do that everything work until I want to access this array.

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

我仍然不清楚如何操作这个破烂的数组,如何访问特定的值...谢谢您的帮助!

It's still unclear for me how to manipulate this ragged array, how do I access a specific value... thanks for any help !

推荐答案

您的代码包含许多错误:

Your code contains many errors:

  1. 您应先分配raggar%vectors,然后再分配其组件raggar%vectors%elements.
  2. raggar是一个包含可分配数组的标量,该可分配数组包含可分配数组,它不是数组,如果要访问其元素,则只能使用raggar%vectors(i)%elements(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

这篇关于衣衫array的数组中访问的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆