开发者

Creating scala.collection.immutable.vector in Java and adding elements to it [duplicate]

This quest开发者_运维知识库ion already has answers here: Closed 11 years ago.

Possible Duplicate:

Can I use scala List directly in Java ?

Can someone help me with creating scala vector within Java and adding say 50 elements to it? Thanks


If you want to do this efficiently, you'll need to use a builder:

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);
    }
}


import scala.collection.immutable.Vector;

Vector v = Vector.empty();
for(int i = 1; i <51; i++)
{
    v = v.appendBack(i);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜