开发者

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

开发者 https://www.devze.com 2023-03-15 18:22 出处:网络
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?

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

精彩评论

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