ClojureDocs

Nav

Namespaces

into-array

clojure.core

Available since 1.0 (source)
  • (into-array aseq)
  • (into-array type aseq)
Returns an array with components set to the values in aseq. The array's
component type is type if provided, or the type of the first value in
aseq if present, or Object. All values in aseq must be compatible with
the component type. Class objects for the primitive types can be obtained
using, e.g., Integer/TYPE.
3 Examples
;; Array's component type is set to (class 2), cannot add Strings.
;; This will result in an IllegalArgumentException
user=> (into-array [2 "4" "8" 5])
;; Evaluation aborted.

;; However, if the common type is specified, aforementioned values can be put into an array
user=> (into-array Object [2 "4" "8" 5])
#<Object[] [Ljava.lang.Object;@3aa6d0a4>

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

;; if you assign a type, you still have to coerce values
user=> (into-array Byte/TYPE (range 4))
;; Evaluation aborted.

user=> (into-array Byte/TYPE (map byte (range 4)))
#<byte[] [B@68ffefc9>
;; java.nio.file.Paths#get takes (String, String...)
user=> (java.nio.file.Paths/get "/Users" (into-array ["username" "dev" "clojure"]))
#<UnixPath /Users/username/dev/clojure>
;; Creating an empty array defaults to Object[]
user=> (into-array [])
#<Object[] [Ljava.lang.Object;@21f1151f>

;; However, the type of an empty array can be coerced
user=> (into-array String [])
#<String[] [Ljava.lang.String;@578baf24>
See Also

Returns an array of Objects containing the contents of coll, which can be any Collection. Maps to...

Added by alilee

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

Added by alilee
0 Notes
No notes for into-array