简体   繁体   中英

How do I create a primitive two-dimensional (2d) array of doubles in Clojure?

A Java API I am Clojure interoping with requires that I pass it a 2d array of doubles; double[][]. How do I create a primitive 2d array of doubles in Clojure? I am looking for something like this

(double-array-2d [[1 2] [3 4]])

This function would have a Java return type of double[][].

试试这个:

(into-array (map double-array [[1 2] [3 4]]))

Try this:

(defn double-array-2d [coll]
  (let [w (count coll)
        h (apply max (map count coll))
        arr (make-array Double/TYPE w h)]
    (doseq [x (range w)
            y (range h)]
      (aset arr x y (double (get-in coll [x y]))))
    arr))

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