简体   繁体   English

在 Clojure 中从特定于类型的数组创建通用数组

[英]Create generic array from type specific arrays in Clojure

I am working with JFreeChart in clojure and came across a situation I'd like help.我正在 clojure 中使用 JFreeChart,遇到了需要帮助的情况。

JFreeChart's DefaultXYDataset has a method addSeries which is used to add series to the chart. JFreeChart 的 DefaultXYDataset 有一个 addSeries 方法,用于向图表添加系列。 The data is supposed to be an array containing two arrays which are type specific(Array of doubles).数据应该是一个包含两个特定类型数组(双精度数组)的数组。 I therefore wrote the following code thinking it would work but i get a ClassCastException that class [Ljava.lang.Object; cannot be cast to class [[D ([Ljava.lang.Object; and [[D are in module java.base of loader 'bootstrap')因此,我编写了以下代码,认为它可以工作,但我得到了ClassCastException class [Ljava.lang.Object; cannot be cast to class [[D ([Ljava.lang.Object; and [[D are in module java.base of loader 'bootstrap') class [Ljava.lang.Object; cannot be cast to class [[D ([Ljava.lang.Object; and [[D are in module java.base of loader 'bootstrap') . class [Ljava.lang.Object; cannot be cast to class [[D ([Ljava.lang.Object; and [[D are in module java.base of loader 'bootstrap')

(doto _dataset
  (.addSeries "S1" (to-array (conj
                               []
                               (double-array (range 10))
                               (double-array (range 10))))))

After looking through i realized that to-array converts the two nested arrays to #object["[Ljava.lang.Object;" 0x491223e7 "[Ljava.lang.Object;@491223e7"]在查看之后我意识到to-array将两个嵌套to-array转换为#object["[Ljava.lang.Object;" 0x491223e7 "[Ljava.lang.Object;@491223e7"] #object["[Ljava.lang.Object;" 0x491223e7 "[Ljava.lang.Object;@491223e7"] instead of the intended #object["[D" 0x4f5cf37 "[D@4f5cf37"] #object["[D" 0x6d895193 "[D@6d895193"] . #object["[Ljava.lang.Object;" 0x491223e7 "[Ljava.lang.Object;@491223e7"]而不是预期的#object["[D" 0x4f5cf37 "[D@4f5cf37"] #object["[D" 0x6d895193 "[D@6d895193"] Is there a way of combining them to a generic array without converting them to arrays of longs?有没有办法将它们组合到通用数组而不将它们转换为 long 数组? Maybe another method apart from to-array .也许除了to-array之外的另一种方法。 Any other suggestions are welcome.欢迎任何其他建议。 Thanks.谢谢。

Edit: @bfabry answer will work, i could use make-array and then use aset-double but this will result in me looping through the two sequences and assign their values to the main array.编辑:@bfabry 答案会起作用,我可以使用make-array然后使用aset-double但这将导致我遍历两个序列并将它们的值分配给主数组。 I am trying to avoid this as the two datasets can be quite big, even up to 300k items each.我试图避免这种情况,因为这两个数据集可能非常大,每个数据集甚至高达30 万个项目

That's two dimensional array, not an array of two array objects.那是二维数组,而不是两个数组对象的数组。 You'll need to use make-array and aset-double to make the array you want.您需要使用make-arrayaset-double来制作您想要的数组。

user=> (class (make-array Double/TYPE 2 2))
[[D

https://clojuredocs.org/clojure.core/make-array https://clojuredocs.org/clojure.core/make-array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM