Can I declare and initialize an array with the sam

2019-02-28 10:35发布

Is there a way to do the following at the same time?

static final int UN = 0; // uninitialized nodes
int[] arr;

// ... code ...

arr = new int[size];
for (int i = 0; i < 5; i++) {
    arr[i] = UN;
}

Basically, I want to declare arr once I know what its size will be and initialize it to UN without having to loop. So something like this:

int[] arr = new int[size] = UN;

Is this possible?

Thanks.

7条回答
Explosion°爆炸
2楼-- · 2019-02-28 11:08
int arr[] = { 0, 0, 0, 0, 0 };
查看更多
登录 后发表回答