简体   繁体   English

for循环错误中访问Array.items []

[英]Accessing Array.items[] in a for-loop error

With the aim of getting a better performance I'm fine tuning the code, looking through the DDMS tracer. 为了获得更好的性能,我通过DDMS跟踪器对代码进行了微调。 One aspect is Array.get(x) which is more expensive than Array.items[x] 一方面是Array.get(x) ,它比Array.items[x]昂贵

We can directly access the items proving the array type is Object, or, we specify the array type in the constructor, like so: 我们可以直接访问items证明了数组类型为对象,或者,我们在构造函数中指定数组类型,就像这样:

Array<MyClass> foo = new Array<MyClass>(MyClass.class)

This works fine, however, how do I specify the last MyClass.class in a for loop? 这很好,但是,如何在for循环中指定最后一个MyClass.class I have this at the moment: 我现在有这个:

for (Array<MyClass> listOfObjects : allObjects) {

    for (int i=0; i<listOfObjects.size; i++) {

        MyClass myObj = listOfObjects.get(i);   
        //MyClass myObj = listOfObjects.items[i];  

The commented line works fine, but trying to get rid of the overhead, I want to supply the `(MyClass.class)' like mentioned above. 带注释的行工作正常,但是为了摆脱开销,我想像上面提到的那样提供`(MyClass.class)'。 Where can I do this in that for-loop constructor? 在那个for循环构造函数中我可以在哪里做呢?

Many thanks J 非常感谢J

I think that what you're trying to do is pointless. 我认为您要尝试做的事毫无意义。 Please read this great article: http://blog.codinghorror.com/the-sad-tragedy-of-micro-optimization-theater/ 请阅读这篇精彩的文章: http : //blog.codinghorror.com/the-sad-tragedy-of-micro-optimization-theater/

You are trying to generate some minimal optimization, while at the same time greatly reducing readibility and maintainability. 您试图产生一些最小的优化,同时又大大降低了可读性和可维护性。

If you want less overhead, it would probably be wiser to look at a language like C++, rather than trying to hack basic java for loops. 如果您希望减少开销,那么看一看C ++之类的语言而不是尝试破解基本的Java for循环可能更明智。

Another thing you may want to look into is Java 8, which has added functionality for executing loops concurrently with Streams. 您可能还需要研究的另一件事是Java 8,它增加了与Streams同时执行循环的功能。

Array<MyClass> foo = new Array<MyClass>(MyClass.class)

Note that you are creating a NEW array with this line, passing it a class argument. 请注意,您正在使用此行创建一个NEW数组,并向其传递一个类参数。 From http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Array.html 来自http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Array.html

Array(java.lang.Class arrayType)
Creates an ordered array with items of the specified type and a capacity of 16.

I don't see you trying to create new Arrays in the other code you posted. 我看不到您尝试在发布的其他代码中创建新的数组。 Are you trying to populate each listOfObjects in allObjects ? 您是否要填充listOfObjects中的每个allObjects If so, you would want to do something like: 如果是这样,您将希望执行以下操作:

for (int i = 0; i < allObjects.size; i++)
{
    allObjects.items[i] = new Array<MyClass>(MyClass.class);
}

If you are simply trying to loop through these arrays, there is no class argument needed. 如果您只是试图遍历这些数组,则不需要类参数。 I would suggest comparing the Array class to other Gdx or Java collections if the speed of iteration is too slow. 如果迭代速度太慢,我建议将Array类与其他Gdx或Java集合进行比较。

This quote from above link may also be notable if you do a lot of removing from the arrays. 如果您从数组中进行了大量删除操作,则上述链接中的引号也可能很引人注目。

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).

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

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