Java “为什么我会出错”;类型不匹配“;?
当我试图在主类中初始化构造函数时,我得到了错误类型不匹配Java “为什么我会出错”;类型不匹配“;?,java,Java,当我试图在主类中初始化构造函数时,我得到了错误类型不匹配 public class Test_array { int arr[]; Test_array() { arr=new arr[]; } public static void main(String[]args) { } } class RecTest { int values[]; RecTest() { values = new int[
public class Test_array
{
int arr[];
Test_array()
{
arr=new arr[];
}
public static void main(String[]args)
{
}
}
class RecTest
{
int values[];
RecTest()
{
values = new int[10];
}
}
arr
是变量名而不是类型
arr = new int[10];
arr
是变量名而不是类型
arr = new int[10];
但是在第二个类RecTest中,对于value=newint[10],我没有得到任何错误;我们可以将变量初始化为构造函数。这是因为您在分配新数组时使用了正确的语法类型。在这种情况下,我们可以在main方法中使用构造函数初始化变量吗?您已经在构造函数中初始化了数组(至少是第二个)但是在第二个类RecTest中,对于value=newint[10],我没有得到任何错误;我们可以将变量初始化为构造函数。这是因为您在分配新数组时使用了正确的语法类型。在这种情况下,我们可以在main方法中使用构造函数初始化变量吗?您已经在构造函数中初始化了数组(至少是第二个)