gpt4 book ai didi

java - 此处不允许使用数组初始值设定项

转载 作者:IT老高 更新时间:2023-10-28 20:56:54 26 4
gpt4 key购买 nike

我正在开发 Android 项目,但遇到了一个我无法理解的错误:

Array initializer is not allowed here

我试图简化我的代码,结果就是这样

public class MainActivity extends Activity{

int pos = {0, 1, 2};

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pos = {2, 1, 0};
}
}

这是怎么回事?

最佳答案

你应该使用

pos = new int[]{1,2,3};

您只能在变量初始化时使用缩写语法int[] pos = {0,1,2};

private int[] values1 = new int[]{1,2,3,4};
private int[] values2 = {1,2,3,4}; // short form is allowed only at variable initialization

关于java - 此处不允许使用数组初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41658497/

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