简体   繁体   English

Clojure-将列表转换为Java数组

[英]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: 该文档( http://clojuredocs.org/clojure_core/clojure.core/into-array )包含在非向量序列上使用入数组的示例:

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作为参数。

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

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