Java - 实现动态大小的对象数组的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - best way to implement a dynamic-size array of objects
提问by dan
I am a novice with Java.
我是Java新手。
I have to implement an array of objects that changes in size during execution.
我必须实现一个在执行过程中大小会发生变化的对象数组。
The code I am writing is going to be ported on Android, too.
我正在编写的代码也将移植到 Android 上。
According to your experience, what's the best class to implement that?
根据您的经验,实现它的最佳类是什么?
Thanks,
Dan
谢谢,
丹
采纳答案by Dov
Java has an inadequate template capability. As long as you want an array of objects, then ArrayList<T>
is good. For primitives, it's awful.
Java 的模板功能不足。只要你想要一个对象数组,那就ArrayList<T>
好。对于原始人来说,这是可怕的。
Assuming you have a hierarchy of objects that you want to put in a list, ArrayList
is ideal:
假设您有一个要放入列表的对象层次结构,ArrayList
是理想的:
ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();
vehicles.add(new Car(...));
vehicles.add(new Truck(...));
I'm assuming in the above example that Vehicle is the base class, and Car and Truck are subclasses.
我在上面的例子中假设 Vehicle 是基类,Car 和 Truck 是子类。
On the other hand, if you want a list of numbers, Java is highly inefficient. Each object is a reference (really a 4 byte pointer) to a 12 byte chunk of memory, plus what you're actually using. Since ArrayList cannot apply to int, this means that creating a list of numbers means:
另一方面,如果你想要一个数字列表,Java 是非常低效的。每个对象都是对 12 字节内存块的引用(实际上是 4 字节指针),加上您实际使用的内容。由于 ArrayList 不能应用于 int,这意味着创建数字列表意味着:
- Creating a list of Integer, the object wrapper for int.
- Converting the objects every time you pull out a number. This is done automatically these days, but it takes time.
- Initializing 5 times as much storage as needed.
- 创建一个 Integer 列表,它是 int 的对象包装器。
- 每次拉出一个数字时转换对象。这几天是自动完成的,但需要时间。
- 根据需要初始化 5 倍的存储空间。
So, if you are manipulating big chunks of primitive data (int, float, double) it can be worth your while writing your own version of ArrayList. This is particularly important when the data is big and the platform is small (like a handheld Android thingy).
因此,如果您正在处理大量原始数据(int、float、double),那么编写您自己的 ArrayList 版本可能是值得的。当数据大而平台小(如手持 Android 设备)时,这一点尤为重要。
Compare this:
比较一下:
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000000; i++)
list.add(i):
to:
到:
public class IntArray {
private int[] data;
private int used;
private void grow() {
// implement code to make data double in size here...
}
public IntArray(int size) {
data = new int[size];
used = 0;
}
public void add(int i) {
if (i >= data.length) grow();
data[used++] = i;
}
}
IntArray list2 = new IntArray(1000000);
for (int i = 0; i < 1000000; i++)
list2.add(i);
The last time I benchmarked it, the optimal use of the primitive list is more than 10 times faster than the admittedly suboptimal use of ArrayList. To be more fair, pre-allocate the arraylist to be the right size -- it's still way slower.
上次我对它进行基准测试时,原始列表的最佳使用比 ArrayList 的公认次优使用快 10 倍以上。更公平地说,将 arraylist 预先分配为正确的大小——它仍然要慢得多。
LinkedList is only worthwhile if you are inserting in the beginning or middle of a list. If your list is being built by adding to the end, ArrayList will thoroughly dominate LinkedList. So for a typical list of objects which you are building up in order, ArrayList is what you are looking for. For a big list of primitives like int or double, write your own list.
LinkedList 仅在您插入列表的开头或中间时才有价值。如果您的列表是通过添加到末尾来构建的,则 ArrayList 将彻底支配 LinkedList。因此,对于按顺序构建的典型对象列表,ArrayList 就是您要查找的对象。对于像 int 或 double 这样的大量原语,请编写您自己的列表。
回答by Quintin Robinson
You would probably be most interested in ArrayList
.
您可能对ArrayList
.
I think even wikipedia has info on using generic lists in java: http://en.wikipedia.org/wiki/Generics_in_Java
我认为甚至维基百科都有关于在 Java 中使用泛型列表的信息:http: //en.wikipedia.org/wiki/Generics_in_Java
回答by Amir Raminfar
You should familiar your self with the collection frameworkin java. You have things like Set, List, Map, SortedSets, etc... Which can be really helpful data structures.
您应该熟悉Java 中的集合框架。你有像 Set、List、Map、SortedSets 之类的东西……它们是非常有用的数据结构。
回答by Anton
It depends on what you're gonna use it for.
这取决于你要用它做什么。
If you have an array that changes size frequently, then I'd recommend using an ArrayListor something else from the collection framework(depending on what you're gonna use it for).
如果您有一个经常更改大小的数组,那么我建议您使用ArrayList或集合框架中的其他内容(取决于您要使用它的目的)。
If you however have an array that changes size very infrequently but is read frequently, it would probably be faster to use a regular array and then resize it when needed instead
但是,如果您有一个很少更改大小但经常读取的数组,则使用常规数组然后在需要时调整其大小可能会更快
Arrays.copyOf(array, newSize);
Hope it helps! :)
希望能帮助到你!:)