此处不允许使用数组初始值设定项 [英] Array initializer is not allowed here

查看:73
本文介绍了此处不允许使用数组初始值设定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on Android project and I am getting an error which I cannot understand:

此处不允许使用数组初始化器

Array initializer is not allowed here

我试图简化我的代码,归结为这个

I tried to simplify my code and it comes down to this

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};.

You can only use the abbreviated syntax int[] pos = {0,1,2}; at variable initialization time.

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

这篇关于此处不允许使用数组初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆