Clojure为什么要设置更改列

时间:2018-10-07 07:39:03

标签: arrays clojure

我尝试使用aset更改二维数组中的一个元素,但它更改了列。我该如何解决?

我创建了double[][]数组:

user=> (def z-buffer
    (into-array (repeat 5 (double-array 5 Double/MIN_VALUE))))
user=> (pprint z-buffer)
[[4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324]]
nil

应更改元素的方法:

user=> (defn z-buffer-check [x y z]
  (let [val (double z)]
    (if (> val (aget z-buffer x y))
      (do
        (aset z-buffer x y val)
        true)
      false)))

我的动作:

user=> (z-buffer-check 2 2 6.6)
true
user=> (pprint z-buffer)

预期结果:

[[4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 6.6, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324, 4.9E-324]]
nil

实际结果:

[[4.9E-324, 4.9E-324, 6.6, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 6.6, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 6.6, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 6.6, 4.9E-324, 4.9E-324],
 [4.9E-324, 4.9E-324, 6.6, 4.9E-324, 4.9E-324]]
nil

1 个答案:

答案 0 :(得分:2)

设置z缓冲区时,您需要返回新结果。因此,不要使用repeat,请使用:

(repeatedly 5 #(double-array 5 Double/MIN_VALUE)))