本文介绍了如何在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 [];
这对吗?
推荐答案
反之亦然:
Vehicle[] car = new Vehicle[N];
这更有意义,因为数组中的元素数量不是数组类型的一部分 car
,但它是数组初始化的一部分,该数组最初是您为其分配给 car 。然后,您可以在另一条语句中重新分配它:
This makes more sense, as the number of elements in the array isn't part of the type of
car
, but it is part of the initialization of the array whose reference you're initially assigning to car
. You can then reassign it in another statement:
car = new Vehicle[10]; // Creates a new array
(请注意,我已更改类型名称以匹配Java命名约定。)
(Note that I've changed the type name to match Java naming conventions.)
有关数组的更多信息,请参见。
For further information about arrays, see section 10 of the Java Language Specification.
这篇关于如何在Java中声明对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!