我可以声明和初始化在Java中相同的指令数组? [英] Can I declare and initialize an array with the same instruction in Java?
问题描述
有没有办法做到在同一时间下?
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;
这可能吗?
感谢。
推荐答案
没有,不符合标准库。如果你编写自己的函数,不过,你可以很容易地做到这一点在一条语句(不是指令;这些是不同的)。矿山看起来像的String [] [] =字符串Arrayu.fill(新的String [X] [Y],);
No, not with the standard libraries. If you write your own functions, though, you can easily do so in a single statement (not instruction; those are different). Mine looks like String[][] strings = Arrayu.fill(new String[x][y], "");
下面是一个链接。这里也有一些垃圾信息太多,虽然,我刚刚发布了电流源副本的情况下直接清除它。
Here's a link. There's some junk in there too, though; I just posted a copy of the current source directly without cleaning it up.
这篇关于我可以声明和初始化在Java中相同的指令数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!