libGDX Array类的好处 [英] libGDX Array class benefits

查看:97
本文介绍了libGDX Array类的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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集合类的唯一替代".还有更多类似ObjectSetIntIntMap的内容.您可以在此处 a>.

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<Integer, Integer>无法处理原语,这会导致许多自动装箱的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆