How do you declare an array of objects in Java?

Possible duplicate:
How to declare an array in Java?

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

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

Is it correct?

+6
source share
4 answers

This is the opposite:

 Vehicle[] car = new Vehicle[N]; 

This is more important because the number of elements in the array is not part of the car type, but it is part of the initialization of the array, the reference to which you originally assigned to car . Then you can reassign it in another statement:

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

(Note that I changed the type name in accordance with the Java naming conventions.)

For more information about arrays, see section 10 of the Java Language Specification .

+15
source

Like this

Vehicle[] car = new Vehicle[10];

+3
source
 vehicle[] car = new vehicle[N]; 
+2
source

This is the correct way:

You must declare the length of the array after "="

 Veicle[] cars = new Veicle[N]; 
0
source

Source: https://habr.com/ru/post/907593/


All Articles