Search code examples
clojureclojure-java-interop

aget returns a java object when applied to a `make-array` array


I expect the following code to return the (0,0) value. Yet I get a java object

(let [axs (make-array Long 5 5 0)]                                                                                                                                                                                                                                              
  (aget axs 0 0))        

Also I got a type mismatch when trying to mutate index (0,0)

(let [axs (make-array Long 5 5 0)]                                                                                                                                                                                                                                              
  (aset axs 0 0 1))  

I expect to have initialized a Long[5][5] java array to zero. What am I missing here?

Thanks


Solution

  • Third argument to make-array doesn't initialize your array with zeros, but adds third array dimension, which size is equal to zero. As a result, the size of your array is zero as well. To fix the issue, create your array as follows:

    (make-array Long/TYPE 5 5)
    

    This will create array of primitive longs and will initialize it with zeros.