英文:
What is wrong with the way I have initialized 'temps'?
问题
public class ArrayMethod {
static int[] temps;
public static void create() {
temps = new int[]{28, 27, 28, 21, 21, 19, 18, 29, 31, 24};
}
}
I am required to use 'temps' in other methods but am getting a variety of errors.
英文:
public class ArrayMethod {
static int[] temps;
public static void create() {
temps = {28, 27, 28, 21, 21, 19, 18, 29, 31, 24};
}
}
I am required to use 'temps' in other methods but am getting a variety of errors.
答案1
得分: 0
用以下代码初始化数组:
temps = new int[] {...};
英文:
to initialize array, use
temps = new int[] {...};
答案2
得分: 0
用以下方式初始化你的数组。你现在的使用方式会导致编译时错误,因为数组常量只能在初始化器中使用。
temps = new int[]{28, 27, 28, 21, 21, 19, 18, 29, 31, 24};
英文:
initialize your array as below. The way you are using would give compile time error because array constants can only be used in initializers
temps = new int[]{28, 27, 28, 21, 21, 19, 18, 29, 31, 24};
专注分享java语言的经验与见解,让所有开发者获益!
评论