Fortran中的零索引数组是什么?请附示例详解
Hey there! Great question—since Fortran is traditionally 1-indexed for arrays, it’s easy to overlook how to set up zero-indexed ones, especially if you’re coming from languages like C or Python where zero-indexing is the norm. Let me break this down clearly for you.
Unlike some languages that force a fixed indexing scheme, Fortran lets you explicitly define the upper and lower bounds of your arrays. To create a zero-indexed array, you just set the lower bound of the array to 0 when declaring it.
1. 静态零索引数组(编译时确定大小)
The simplest way is to specify the index range directly in the array declaration using the (lower:upper) syntax.
一维零索引数组示例
program zero_index_demo implicit none ! 声明一个索引从0到4的整数数组(共5个元素) integer :: my_arr(0:4) integer :: idx ! 给数组元素赋值 do idx = 0, 4 my_arr(idx) = idx * 10 ! arr(0)=0, arr(1)=10, ..., arr(4)=40 end do ! 打印结果 print *, "One-dimensional zero-indexed array:" do idx = 0, 4 print *, "my_arr(", idx, ") = ", my_arr(idx) end do end program zero_index_demo
When you run this, it will output each element using its zero-based index—no tricks required.
多维零索引数组示例
You can apply the same logic to multi-dimensional arrays, setting the lower bound of any (or all) dimensions to 0:
program multi_dim_zero_index implicit none ! 二维数组:第一维索引0-2,第二维索引0-1(共3×2=6个元素) real :: my_matrix(0:2, 0:1) integer :: i, j ! 填充数组 do i = 0, 2 do j = 0, 1 my_matrix(i,j) = i + j*0.5 end do end do ! 打印矩阵 print *, "Two-dimensional zero-indexed matrix:" do i = 0, 2 print *, "Row ", i, ": ", my_matrix(i,0), " | ", my_matrix(i,1) end do end program multi_dim_zero_index
2. 动态零索引数组(运行时确定大小)
If you need an array whose size is determined at runtime, use allocate() and specify the zero-based bounds when allocating:
program dynamic_zero_index implicit none integer, allocatable :: dyn_arr(:) integer :: arr_size, idx arr_size = 4 ! 我们想要4个元素,索引0-3 allocate(dyn_arr(0:arr_size-1)) ! 下界0,上界arr_size-1 ! 赋值 do idx = 0, arr_size-1 dyn_arr(idx) = idx**2 ! arr(0)=0, arr(1)=1, arr(2)=4, arr(3)=9 end do print *, "Dynamic zero-indexed array:" print *, dyn_arr ! 记得释放动态数组 deallocate(dyn_arr) end program dynamic_zero_index
关键注意事项
- Zero-indexed arrays are fully compliant with Fortran standards—all array operations (slicing, passing to subroutines, etc.) work exactly as you’d expect, as long as you use the correct index range.
- When passing a zero-indexed array to a subroutine, use assumed-shape arrays (like
integer :: arr(:)) if you want the subroutine to handle any index bounds. You can uselbound()andubound()to get the actual lower/upper bounds of the array inside the subroutine:
This subroutine will work seamlessly with both 1-indexed and 0-indexed arrays.subroutine print_any_array(arr) implicit none integer :: arr(:) integer :: idx, lower, upper lower = lbound(arr, 1) upper = ubound(arr, 1) print *, "Array bounds: ", lower, " to ", upper do idx = lower, upper print *, "arr(", idx, ") = ", arr(idx) end do end subroutine print_any_array
内容的提问来源于stack exchange,提问作者Prashant Shukla

