Array initializer is not allowed here

Abdurakhmon :

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

What is going on here?

Jayanth :

You should use

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

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=425037&siteId=1