java如何声明对象,如何在Java中声明对象数组?

Suppose I have an object car (class vehicle) and I want to create an array of N number of cars . How do I declare that in Java?

vehicle[N]= car=new vehicle [];

Is this right?

解决方案

Its the other way round:

Vehicle[] car = new Vehicle[N];

This makes more sense, as the number of elements in the array isnt part of the type of car, but it is part of the initialization of the array whose reference youre initially assigning to car. You can then reassign it in another statement:

car = new Vehicle[10]; // Creates a new array

(Note that Ive changed the type name to match Java naming conventions.)

Suppose I have an object car (class vehicle) and I want to create an array of N number of cars . How do I declare that in Java? vehicle[N]= car=new vehicle []; Is this right? 解决方案 Its the other way round: Vehicle[] car = new Vehicle[N]; This makes more sense, as the number of elements in the array isnt part of the type of car, but it is part of the initialization of the array whose reference youre initially assigning to car. You can then reassign it in another statement: car = new Vehicle[10]; // Creates a new array (Note that Ive changed the type name to match Java naming conventions.)
经验分享 程序员 微信小程序 职场和发展