简体   繁体   English

没有硬编码大小的Java数组声明

[英]Java array declaration without hard coding the size

如何在不对其大小进行硬编码的情况下初始化另一个类中的类的对象数组?

Use a List . 使用列表 The size does not need to be declared on creation of the List. 在创建List时不需要声明大小。 The toArray() method will return an array representation of the list. toArray()方法将返回列表的数组表示形式。 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: 例如:

public String[] createArray(int size) {
    // Not hard-coded, but array is not expandable
    return new String[size];
}

If you want a collection which can grow an shrink over time, look at the various List<E> implementations, such as ArrayList<E> . 如果您想要一个可以随着时间的推移而缩小的集合,请查看各种List<E>实现,例如ArrayList<E>

Arrays are fixed in length. 数组的长度是固定的。 I would recommend using a Collection. 我建议使用Collection。

Here is an article on collections: 这是一篇关于馆藏的文章:

http://en.wikipedia.org/wiki/Java_collections_framework http://en.wikipedia.org/wiki/Java_collections_framework

With these, you can add elements by using an Add() command or something similar. 使用这些,您可以使用Add()命令或类似的东西添加元素。

As mentioned in the previous answers, an ArrayList or List are collections. 如前面的答案中所述,ArrayList或List是集合。

Object[] will always be fixed size. 对象[]将始终是固定大小。 If you need a variable length collection, try ArrayList, LinkedList, or one of the many others. 如果需要可变长度集合,请尝试使用ArrayList,LinkedList或其他许多集合。

Pick the collection carefully, since they all have different performance aspects. 仔细挑选系列,因为它们都有不同的性能方面。

For mutable arrays other container objects are used. 对于可变数组,使用其他容器对象。

When using a set of objects, an ArrayList or Vector object is used. 使用一组对象时,使用ArrayList或Vector对象。 You can also store objects with an object key eg "Name" = "Ben" instead of [0] = "Ben". 您还可以使用对象键存储对象,例如“Name”=“Ben”而不是[0] =“Ben”。

Vector v = new Vector();
for(int i = 0; i < 100; i++){
 Object o = new Object();
 // init object
 v.addElement(o);
}

for(int i = 0; i < 100; i++){
 Object o = v.elementAt(i);
 // manipulate object
}

Now you have an arbritairy list of object of undefined length. 现在你有一个未定义长度的对象的arbritairy列表。 Size found by using vector.size() method. 使用vector.size()方法找到的大小。

java.util package is required and part of J2SE 1.3 and higher. java.util包是必需的,是J2SE 1.3及更高版本的一部分。

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. 使它比您需要的大,将未使用的条目保留为null。 You may want to keep a "slotsUsed" variable. 您可能希望保留“slotsUsed”变量。

  • 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. 您可以创建一个新数组并像这样初始化它。

String[] strArray = {"Initialize","Array","Like","This"};

If you want an array with a dynamic size I would recommend using an ArrayList. 如果你想要一个动态大小的数组,我建议使用ArrayList。

If you want an array of primitive instead of objects, you can use Trove4j. 如果你想要一个原始数组而不是对象,你可以使用Trove4j。 Otherwise use an ArrayList, or CopyOnWriteArrayList to wrap an array. 否则使用ArrayList或CopyOnWriteArrayList来包装数组。 There are other List implementations but these do not act like arrays for access time. 还有其他List实现,但这些实现不像访问时间的数组。

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 有时,如果您知道应用程序需要的对象的上限,则将数组的大小声明为有用

static final int ARRAY_SIZE = 1000;

This goes near the beginning of the class so it can be easily changed. 这接近课程的开头,因此可以很容易地改变。 In the main code instantiate the array with 在主代码中实例化数组

Object[] objects = new Object[ARRAY_SIZE];

Also in case the array you want to use has the same size as another array consider using 此外,如果您要使用的阵列与另一个阵列的大小相同,请考虑使用

Object[] objects = new Object[other_objects.length];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM