本文介绍了为什么我收到有关Java数组分配的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将int常量分配给数组的certian索引.我不断从编译器中获取错误.文章似乎暗示我已经正确地做到了.这是代码:
I am trying to assign int literals to certian indexs of the array. I keep getting errors from the compiler. This article seems to suggest That I have done this correctly. Here is the code:
public class Mytest {
int[] jim = new int[9];
jim[0] = 77;
}
编译器错误:
Mytest.java:5: error: ']' expected
jim[0] = 77;
^
Mytest.java:5: error: ';' expected
jim[0] = 77;
^
Mytest.java:5: error: illegal start of type
jim[0] = 77;
^
Mytest.java:5: error: <identifier> expected
jim[0] = 77;
^
先谢谢了.
推荐答案
您需要将分配语句移至有效位置.这可以是方法,构造函数或初始化块.
You need to move your assignment statement to a valid location. This could be a method, a constructor, or an initialization block.
public class Mytest {
int[] jim = new int[9];
// Using an initialization block
{
jim[0] = 77;
}
}
这篇关于为什么我收到有关Java数组分配的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!