gpt4 book ai didi

java - 在 Java 中,在方法中初始化和访问数组?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:39:46 24 4
gpt4 key购买 nike

<分区>

 class example{

int[] quiz = new int[] { 10 , 20 }; //location 1

public static void main(String[] args) {

int[] test = new int[2]; // location 2
test[0] = 2;
test[1] = 3;
// other code
}

以上代码运行正确。但是下面的代码会导致错误。我对错误的推理是因为 quiz 是在需要立即初始化的方法之外声明的。但是我不确定这是否是正确的解释。

class example{

int[] quiz = new int[2]; //location of error
quiz[0] = 10;
quiz[1] = 20;


public static void main(String[] args) {

int[] test = new int[2]; // location 2
test[0] = 2;
test[1] = 3;
//other code
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com