ClojureDocs

Nav

Namespaces

to-array

clojure.core

Available since 1.0 (source)
  • (to-array coll)
Returns an array of Objects containing the contents of coll, which
can be any Collection.  Maps to java.util.Collection.toArray().
3 Examples
user=> (to-array [1 2 3])
#<Object[] [Ljava.lang.Object;@b2bb14>
(def hello (to-array "Hello World!"))

(aget hello 1)
;; => \e

(aset hello 1 \b) ;;Mutability! Watch out!
;; => \b

(dotimes [n (alength hello)] (print (aget hello n)))
;; => Hbllo World!

;; Calling `to-array` on array returns the original, not a copy
(identical? (to-array hello) hello)
;; => true
user> (class '(1 2 3 4))
;=> clojure.lang.PersistentList

user> (class (to-array '(1 2 3 4)))
;=> [Ljava.lang.Object;

user> (get (to-array '(1 2 3 4)) 0)
;=> 1
See Also

Returns the length of the Java array. Works on arrays of all types.

Added by Claj

Creates an array of chars

Added by Claj

Creates an array of ints

Added by Claj

Creates an array of longs

Added by Claj

Returns an array with components set to the values in aseq. The array's component type is type if ...

Added by alilee

Creates and returns an array of instances of the specified class of the specified dimension(s). N...

Added by alilee

Returns a (potentially-ragged) 2-dimensional array of Objects containing the contents of coll, whi...

Added by alilee
0 Notes
No notes for to-array