我想知道是否可以构建一个 int 名称变量数组。下面的示例,我希望它们初始化为 0;
int[] h1i1ctrlk, h1i2ctrlk, h1i3ctrlk, h1i4ctrlk, h1i5ctrlk;
最佳答案
I'm not sure if it is correct way to create the array and I can't initialise it.
尝试
int[] h1i1ctrlk, h1i2ctrlk, h1i3ctrlk, h1i4ctrlk, h1i5ctrlk;
h1i1ctrlk = new int[length]; // an array of length full of 0s.
....
h1i5ctrlk = new int[length]; // an array of length full of 0s.
将数组的所有元素设置为值 1.0
Arrays.fill(hii1ctrlk, 1.0);
如果您有特定的值,您可以使用
int[] h1i1ctrlk = { 0, 1, 2, 10 };
或
int[] h1i1ctrlk;
hii1ctrlk = new int[] { 0, 1, 2, 10 };
或者使用循环
int[] h1i1ctrlk = new int[length];
for(int i = 0; i < length; i++)
h1i1ctrlk[i] = i * i;
您还可以使用Map<String, int[]>
像这样
Map<String, int[]> ctrlk = new LinkedHashMap<>();
for(int h = 1; h <= 9; h++) {
for(int i = 1; i <= 5; i++) {
String key = "h"+h+"i"+i;
ctrlk.put(key, new int[length]);
}
}
关于java - 是否可以创建一个 int 名称变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071267/