You're viewing version 1.2.0 of into-array. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

into-array

clojure.core

  • (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 top

  • ;; 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>
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2635 top

(defn into-array
  "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."
  {:added "1.0"}
  ([aseq]
     (clojure.lang.RT/seqToTypedArray (seq aseq)))
  ([type aseq]
     (clojure.lang.RT/seqToTypedArray type (seq aseq))))
Vars in clojure.core/into-array: defn seq type

Comments top

No comments for into-array. Log in to add a comment.