开发者

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

开发者 https://www.devze.com 2023-03-17 07:56 出处:网络
This quest开发者_运维知识库ion already has answers here: Closed 11 years ago. Possible 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

精彩评论

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

关注公众号