[英]How to convert apache.spark.ml.linalg.Vector to arrayList in java?
I am trying to convert apache.spark.ml.linalg.Vector
to ArrayList
in Java. 我正在尝试将apache.spark.ml.linalg.Vector
转换为Java中的ArrayList
。
The source code is like this: 源代码是这样的:
Vector vector = (Vector) row.get(1);
ArrayList<String> vectorList = new ArrayList<String>(vector);
the error is: 错误是:
Cannot resolve constructor
'ArrayList(org.apache.spark.ml.linalg.Vector)'
How can I do this? 我怎样才能做到这一点?
You cannot convert a Vector
to an ArrayList<String>
directly. 您不能直接将Vector
转换为ArrayList<String>
。 Vector
provides a toArray()
method ( documentation ) which returns an array of type double
, using which you can convert it into an ArrayList<String>
as below: Vector
提供了一个toArray()
方法( 文档 ),该方法返回double
类型的数组,您可以使用该方法将其转换为ArrayList<String>
,如下所示:
Vector vector = (Vector) row.get(1);
double[] vectorArray = vector.toArray();
ArrayList<String> vectorList = new ArrayList<>();
for (double vectorElement: vectorArray) {
vectorList.add(String.valueOf(vectorElement));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.