简体   繁体   中英

Clojure - convert list into Java array

是否有任何惯用的方式将Clojure列表转换为Java数组,除了首先将其转换为vector并使用into-array (意味着,除了(into-array (vec my-list)) ,我还不想添加高架)?

Your question seems to be based on a false premise. into-array does not need to take a vector, it takes a seqable value. The documentation ( http://clojuredocs.org/clojure_core/clojure.core/into-array ) contains examples of using into-array on a non-vector sequence:

user=> (into-array (range 4))
#<Integer[] [Ljava.lang.Integer;@63d6dc46>

user=> (type (range 4))
clojure.lang.LazySeq

user=> (doc range)
-------------------------
clojure.core/range
([] [end] [start end] [start end step])
  Returns a lazy seq of nums from start (inclusive) to end
  (exclusive), by step, where start defaults to 0, step to 1, and end
  to infinity.

Calling it on a list works just as well:

user=> (into-array (list 1 2 3))
#<Long[] [Ljava.lang.Long;@138297fe>

由于我需要专门拥有一个整数数组,因此我使用了int-array函数,该函数将list作为参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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