我想知道是否可以构建一个int name变量数组。下面的示例,我希望它们初始化为0;
int[] h1i1ctrlk, h1i2ctrlk, h1i3ctrlk, h1i4ctrlk, h1i5ctrlk;
答案 0 :(得分:1)
我不确定创建数组是否正确,我无法初始化它。
尝试
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]);
}
}
答案 1 :(得分:0)
是的,这是可能的。就像这样:
int[] h1i1ctrlk = {1, 2, 3};
或
int[] h1i1ctrlk = new int[3]; //array length of 3
答案 2 :(得分:0)
请尝试这将为您做一些工作 尝试逐个打印所有数组,我们将用0初始化 并且所有数组长度都是1
public class IntName{
public static void main(String args[])
{
int[] h1i1ctrlk, h1i2ctrlk, h1i3ctrlk, h1i4ctrlk, h1i5ctrlk = {0};
h1i1ctrlk = h1i2ctrlk = h1i3ctrlk = h1i4ctrlk = h1i5ctrlk;
System.out.println(h1i1ctrlk.length);
}
}
答案 3 :(得分:0)
我想我找到了一个int变量数组问题的答案。我尝试了下面的代码,它似乎工作。
int h1i1ctrlk, h1i2ctrlk, h1i3ctrlk, h1i4ctrlk, h1i5ctrlk;
int counter[] = {h1i1ctrlk, h1i2ctrlk, h1i3ctrlk, h1i4ctrlk, h1i5ctrlk};
\\ display the value of int, int that's in array;
tvpwlikectr.setText("" + counter[0]);