How to return index of Objecr array elements in Ju

2019-09-20 05:33发布

i trying return index of selected element of my Vector collection

type Node
       name::AbstractString
       value::Int
       left::Nullable{Node}
       right::Nullable{Node}

       Node(name::AbstractString, value::Int) = new(name, value, Nullable{Node}(), Nullable{Node}())
end

function minimal(nodes::Vector{Node})
                minnode=Nullable{Node}()
                minval = nodes[1].value
                    for f in nodes
                        if f.value< minval
                            minval= f.value
                            minnode = f
                        end
                    end
                return find(nodes .== minnode)
end

Problem is of course find(nodes .== minnode), how can I return index of this element

标签: julia
1条回答
我只想做你的唯一
2楼-- · 2019-09-20 06:14

The function seems to be overkill, wouldn't it be sufficient to do:

findmin([i.value for i in nodes])[2]

e.g. for

nodes = [Node("a",12), Node("b",4),Node("c",-5)]

this returns 3, the index of the smallest node in the node list.

查看更多
登录 后发表回答