Returns a (potentially-ragged) 2-dimensional array of Objects
containing the contents of coll, which can be any Collection of any
Collection.
user> (def a (to-array-2d [[1 2 3][4 5 6]])) #'user/a user> (alength a) 2 user> (alength (aget a 0)) 3 user> (aget a 0 0) 1 user> (aget a 0 1) 2 user> (aget a 0 2) 3 user> (aget a 1 0) 4 user> (aget a 2 0) → ERROR nil user>
;; quick example of a ragged array where the length of each element of the ;; 2d array is unique user=> (def a (to-array-2d [[0][1 2][3 4 5][6 7 8 9]])) #'user/a user=> (map alength [(aget a 0)(aget a 1)(aget a 2)]) (1 2 3) user=>
(defn to-array-2d
"Returns a (potentially-ragged) 2-dimensional array of Objects
containing the contents of coll, which can be any Collection of any
Collection."
{:tag "[[Ljava.lang.Object;"
:added "1.0"
:static true}
[^java.util.Collection coll]
(let [ret (make-array (. Class (forName "[Ljava.lang.Object;")) (. coll (size)))]
(loop [i 0 xs (seq coll)]
(when xs
(aset ret i (to-array (first xs)))
(recur (inc i) (next xs))))
ret))
Comments top
No comments for to-array-2d. Log in to add a comment.