有人可以帮我在Java中创建scala向量并添加50个元素吗?感谢
答案 0 :(得分:5)
如果您想有效地执行此操作,则需要使用构建器:
import scala.collection.immutable.Vector;
import scala.collection.immutable.VectorBuilder;
public class VectorTest {
public static void main(String[] args){
VectorBuilder<Integer> vb = new VectorBuilder<Integer>();
for(int i = 0; i < 50; i++)
vb.$plus$eq(Integer.valueOf(i));
Vector<Integer> vec = vb.result();
System.out.println("vec = " + vec);
}
}
答案 1 :(得分:0)
import scala.collection.immutable.Vector;
Vector v = Vector.empty();
for(int i = 1; i <51; i++)
{
v = v.appendBack(i);
}