我有下面的代码创建一个对象数组,然后尝试添加相同类型的对象。

我收到错误“语法错误,插入“ new ClassType()”以完成表达式”
在线上

vehicles.[index] = nextVehicle;


但是在我看来,这没有问题。从long转换为int是否会引起问题?

long vehicleSize = getVehicleSize();

Vehicle[] vehicles= new Vehicle[(int)vehicleSize];
Vehicle nextVehicle = null;
int offSet = 0;
int index= 0;
while (offSet < vehicleSize) {
     nextVeh = new Vehicle(db, offSet);
     vehicles.[index] = nextVehicle;
     index++;
     offSet += nextVehicle.getSize();
}

最佳答案

vehicles.[index] = nextVehicle;


应该

vehicles[index] = nextVehicle;




nextVeh = new Vehicle(db, offSet);


应该是

nextVehicle = new Vehicle(db, offSet);

07-28 07:34