下面的代码
class student
{
string a;
int b;
student( string a, int b)
{
this.a=a;
this.b=b;
}
}
public static void main( string[] srgs)
{
student [] student;
student=new student[3];
}
上面的代码是用对象名字来定义一个数组,
但是在C++中好象没有用对象名字来定义一个数组。只有Char这样的关键字来定义的呀,
请大家指点一下
class student
{
string a;
int b;
student( string a, int b)
{
this.a=a;
this.b=b;
}
}
public static void main( string[] srgs)
{
student [] student;
student=new student[3];
}
上面的代码是用对象名字来定义一个数组,
但是在C++中好象没有用对象名字来定义一个数组。只有Char这样的关键字来定义的呀,
请大家指点一下
char [] c = new char [3]; //定义了char 类型的数组,数组名字叫c这样说明白吗,跟c++是一样的,只不过不用delete
student=new student[3];这里的student是个数组的名称,而非student对象。
需要用FOR循环进行初始化,不然要抛nullpointexpection
那么和C++好象没有什么区别了
谢谢大家
{
String a;
int b; student( String a, int b)
{
this.a=a;
this.b=b;
} public static void main( String[] srgs)
{
student [] student;
student=new student[3]; for(int i = 0; i < student.length; i++)
{
student[ i ] = new student("Student", i);
System.out.println(student[ i ].a + " , " + student[ i ].b );
}
}
}
按你的程序简单帮你改了一下。
我也是初学者,请各位大侠指正。