libGDX Array 类的好处 [英] libGDX Array class benefits
问题描述
libGDX Array 类的 javadoc 说:可调整大小、有序或无序的对象数组.如果无序,则此类在删除元素时避免内存复制(最后一个元素移动到被删除元素的位置).
元素去除改进是这个类的唯一优点还是还有其他优点?
换句话说 - 如果我根本不打算从列表中删除元素,我可以使用 ArrayList 吗?
The javadoc of libGDX Array class says: A resizable, ordered or unordered array of objects. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
Is the elements removal improvement the only advantage of this class or there are others?
In other words - if I'm not planning to remove elements from my list at all can I live with ArrayList?
推荐答案
Array
实际上并不是标准 Java 集合类的唯一替代品".还有更多类似 ObjectSet
或 IntIntMap
.您可以在 这里找到所有这些.
Array
is actually not the only "replacement" of standard Java collection classes. There are many more like ObjectSet
or IntIntMap
. You can find all of them here.
它们大多经过优化,以尽可能避免垃圾收集.他们以多种方式做到这一点.
They are mostly optimized to avoid garbage collection as much as possible. They do this in many ways.
一种方法是您已经指出的一种方法,即尽可能避免内存复制,例如在删除 Array
中的元素的情况下.
One way is the one you already pointed out, by trying to avoid memory copies when possible, for example in case of a removal of an element in an Array
.
此外,他们重复使用迭代器.标准的 java 集合不这样做,这就是为什么每次迭代集合时都会创建一个新的 Iterator
.
Furthermore they re-use the iterators. The standard java collections do not do this, which is why there will be a new Iterator
being created every time you are iterating over the collection.
另一种方式是使用原语,它避免了由于自动装箱而创建的对象.IntIntMap
例如有 int
键和 int
值.标准的 java HashMap
无法处理将导致许多自动装箱 int
-> Integer
的原语.
Another way is the use of primitives, which avoids the creation of Objects due to autoboxing. IntIntMap
for example has int
keys and int
values. The standard java HashMap<Integer, Integer>
cannot deal with primitives which will result in many autoboxed int
-> Integer
.
您应该始终尽量坚持使用 libgdx 类,尤其是在移动设备上.在桌面上,垃圾收集器通常非常快,以至于您不会注意到它,但即使在那里它也会导致丑陋的 FPS 延迟.
You should always try to stick to the libgdx classes whenever you can, especially on mobile devices. On desktop the garbage collector is usually so fast that you won't notice it, but even there it can result in ugly FPS-lags.
这篇关于libGDX Array 类的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!