开发者

Shortest way to fill a (Tree)Set with Integers?

if I want to make a (Tree)Set and fill开发者_开发技巧 it with 2000 Integers. To start with 0 then add 1,2,3,4...2000. Whats the best way?

I could do

Set set = new TreeSet<Integer>();
        set.add(0);
        set.add(1);
        set.add(2);
//...

or make a while with add(i);i++;

But is there a easier/shorter way?

Thank you!


Not really, there are no bob=[0...2000] shortcuts in Java.

Set set = new TreeSet<Integer>();
for(int i = 0; i <= 2000; ++i)
    set.add(i);


Set set = new TreeSet<Integer>();
// add Integers 0,1,...1999
for(int i=0;i<2000;i++){
  set.add(new Integer(i));
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜