Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

How to return index of Objecr array elements in Julia

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...