简体   繁体   English

如何从数组中选择特定元素?

[英]How to choose specific element from array?

Let's consider code following which creates array in a way following:让我们考虑以下代码,它以以下方式创建数组:

在此处输入图像描述

Function my_fun(a As Double, b As Double, n)

    Dim arr() As Variant
    ReDim arr(n - 1)
    Dim i As Integer

    arr(0) = 0
    arr(1) = 1
    For i = 2 To n - 1
        arr(i) = a * arr(i - 1) + b * arr(i - 2)
    Next i
    
    my_fun = Application.Transpose(arr)
    
End Function

This code returns array in which next elements are next sequence values.此代码返回数组,其中下一个元素是下一个序列值。 How can I make my function to return last element of that array instead of just array?如何让我的 function 返回该数组的最后一个元素而不仅仅是数组? For example:例如:

在此处输入图像描述

In this case code should return 4, because it's the very last element of the array.在这种情况下,代码应该返回 4,因为它是数组的最后一个元素。 Do you know how it can be performed?你知道如何执行吗?

To return the last element of the array:要返回数组的最后一个元素:

my_fun = arr(Ubound(arr))

or since the upper bound is n - 1 :或者因为上限是n - 1

my_fun = arr(n - 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM