Java 数组声明,无需对大小进行硬编码
如何在另一个类中初始化一个类的对象数组而不对其大小进行硬编码?
How can I initialize an array of objects of a class in another class without hardcoding its size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如其他地方所述,数组对象具有固定大小。如果出于某种原因您必须使用数组,您可以使用以下一种或两种技术:
使其大于您的需要,保留未使用的
条目为空。您可能想保留一个“slotsUsed”变量。
当数组太小时,创建一个更大的数组并复制
内容放入其中。
它们都在 ArrayList 内部使用。
As noted elsewhere, an array object has a fixed size. If there's some reason you must use an array, you can use one or both of these techniques:
Make it the larger than you need, leaving the unused
entries null. You may want to keep a "slotsUsed" variable.
When the array gets too small, make a bigger one and copy the
contents into it.
These are both used inside ArrayList.
您可以创建一个新数组并像这样初始化它。
如果您想要一个具有动态大小的数组,我建议使用 ArrayList。
You can create a new array and initialize it like this.
If you want an array with a dynamic size I would recommend using an ArrayList.
如果您想要原始数组而不是对象数组,可以使用 Trove4j。否则,请使用 ArrayList 或 CopyOnWriteArrayList 来包装数组。还有其他 List 实现,但这些实现在访问时间方面不像数组。
If you want an array of primitive instead of objects, you can use Trove4j. Otherwise use an ArrayList, or CopyOnWriteArrayList to wrap an array. There are other List implementations but these do not act like arrays for access time.
有时它很有用,如果您知道应用程序所需的对象的上限,
将数组的大小声明为
这靠近类的开头,因此可以轻松更改。
在主代码中实例化数组
另外,如果您要使用的数组与另一个数组具有相同的大小,请考虑使用
Sometimes it is useful, in case you know an upper bound of the objects your application needs,
to declare the size of an array as
This goes near the beginning of the class so it can be easily changed.
In the main code instantiate the array with
Also in case the array you want to use has the same size as another array consider using
使用列表。创建列表时不需要声明大小。 toArray() 方法将返回列表的数组表示形式。您可以使用多种实现,但最流行的是 ArrayList(尽管最好将实现映射到您的特定情况)。
Use a List. The size does not need to be declared on creation of the List. The toArray() method will return an array representation of the list. There are multiple implementations you can use but the most popular tends to be ArrayList (though it is best to map the implementation to your particular situation).
数组在创建后具有固定的大小。在编译时不需要知道大小,但在创建时需要知道大小。例如:
如果您想要一个可以随时间增长而缩小的集合,请查看各种
列出
实现,例如ArrayList
。Arrays have a fixed size after creation. The size doesn't need to be known at compile-time, but it does need to be known at creation time. For example:
If you want a collection which can grow an shrink over time, look at the various
List<E>
implementations, such asArrayList<E>
.数组的长度是固定的。我建议使用集合。
这是一篇关于集合的文章:
http://en.wikipedia.org/wiki/Java_collections_framework
对于这些,您可以使用 Add() 命令或类似命令来添加元素。
正如前面的答案中提到的,ArrayList 或 List 是集合。
Arrays are fixed in length. I would recommend using a Collection.
Here is an article on collections:
http://en.wikipedia.org/wiki/Java_collections_framework
With these, you can add elements by using an Add() command or something similar.
As mentioned in the previous answers, an ArrayList or List are collections.
Object[] 将始终是固定大小。如果您需要可变长度集合,请尝试 ArrayList、LinkedList 或许多其他集合之一。
请仔细挑选该系列,因为它们都有不同的性能方面。
Object[] will always be fixed size. If you need a variable length collection, try ArrayList, LinkedList, or one of the many others.
Pick the collection carefully, since they all have different performance aspects.
对于可变数组,使用其他容器对象。
当使用一组对象时,使用 ArrayList 或 Vector 对象。
您还可以使用对象键存储对象,例如“Name”=“Ben”而不是[0]=“Ben”。
现在您有一个未定义长度的对象的任意列表。
使用 vector.size() 方法找到大小。
java.util 包是必需的,并且是 J2SE 1.3 及更高版本的一部分。
For mutable arrays other container objects are used.
When using a set of objects, an ArrayList or Vector object is used.
You can also store objects with an object key e.g. "Name" = "Ben" instead of [0] = "Ben".
Now you have an arbritairy list of object of undefined length.
Size found by using vector.size() method.
java.util package is required and part of J2SE 1.3 and higher.