Array error in java [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

This question already has an answer here:

int[][][] inputs;  inputs = new int[10][][];  inputs[0] = new int[1][]; inputs[0][0] = new int[14]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1}; 

This is an excerpt from my program, I have no idea why this is causing an error. Isn't this correct?

Thanks in advance :-)

回答1:

In Eclipse I get a pretty clear error message:

Cannot define dimension expressions when an array initializer is provided.

That means that you can either specify the dimension or the array initializer (i.e. the values). You can't specify both at the same time.

Simply change your last line to

inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1}; 


回答2:

You cannot construct an array with a declared length AND a static initialiser. It must be either one or the other.

change inputs[0][0] = new int[14]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1}; to inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1}; - the length of the new array is implicit because you are explicitly initialising the array with 14 elements.



回答3:

The last line should simply be:

inputs[0][0] = {1,1, etc.}; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!