简体   繁体   English

通过arrayList循环

[英]Cycling through arrayList

Background 背景

I have a sprite (an openGL quad) which 'falls' from the top of the screen to the bottom. 我有一个精灵(一个openGL Quad),它从屏幕的顶部“落”到底部。

When it gets to the bottom of the screen, it's values get reset and it starts again (from top to bottom) but moves along in the X Axis to a new X Position. 当它到达屏幕底部时,其值将被重置,并再次开始(从上到下),但沿X轴移动到新的X位置。

WhatI'm doing is storing the X positions in an ArrayList (there could be just 1 so it falls in the same place over and over or 10 so it moves slightly every time it falls). 我正在做的是将X位置存储在ArrayList中(可能只有1个,所以它一遍又一遍地落在同一位置,或者10个,所以每次跌落时它都会稍微移动)。

Problem 问题

What I can't work out is how to go through this arraylist and set the sprite's new X position to the value held in the 'next' arraylist position while bearing in mind that there might not be a 'next' position. 我无法解决的问题是如何遍历此arraylist并将精灵的新X位置设置为“ next” arraylist位置中所保持的值,同时要记住可能没有“ next”位置。

So....... (and this is Pseudo code)....once sprite has fallen and it's Y position has been reset back to the top of the screen: 所以.......(这是伪代码)....一旦精灵掉落并且其Y位置已重置回屏幕顶部:

spriteX = value_held_in_next_arrayList_position(if_there_is_one)

As different levels of my game will have a different number of positions I can't explicitly say 'go to position 2' incase the arrayList is only a size of 1. Basically what I want is to say 'go to next position' and 'if there isn't a next position' then go back to the position 0. 由于我的游戏的不同级别将具有不同数量的位置,因此在arrayList的大小仅为1的情况下,我无法明确地说“进入位置2”。基本上,我要说的是“进入下一个位置”和“如果没有下一个职位”,则返回到位置0。

ArrayList is iterable, so this is a good place to use an iterator . ArrayList是可迭代的,因此这是使用iterator的好地方。 Assuming that you're not modifying the list while you're iterating. 假设您在迭代时没有修改列表。

You can iterate a List using while with Iterator from the List: 您可以使用whileList使用IteratorIterator列表:

List<YourElement> yourList;
...
Iterator<YourElement> iterator = yourList.iterator();
while(iterator.hasNext()) {
    final YourElement yourElement = iterator.next();        
    // Do whatever with yourElement
    if (iterator.hasNext()) {
        // Do whatever if there's a next element to yourElement
    }
}

What you want to do is get the iterator and then do a while loop where if the iterator has got a next you grab it and carry on, otherwise you come out. 您想要做的是获取迭代器,然后执行while循环,如果迭代器有下一个迭代器,则将其抓住并继续,否则就会退出。

Example code: 示例代码:

Iterator positions = spriteXPositions.iterator();
while(positions.hasNext()) {
    mySprite.position.X = positions.next();
}

I would use an Iterator object. 我将使用Iterator对象。 Since you want it to loop and you don't know the size you want to abstract that from you. 由于您希望它循环播放,而且您不知道要从中抽象出来的大小。 We can make a generic loop method that takes an iterator and the parent ArrayList and will cycle through it if it is already at the end. 我们可以创建一个通用的循环方法,该方法采用一个迭代器和父ArrayList,如果循环已经在末尾,则将循环遍历它。

private void loop(Iterator it, ArrayList parent) {
    if(!it.hasNext()) { 
        it = parent.iterator();
    } 
    //Your logic here instead of logging
    //Assume always at least 1 element in arraylist
    Log.d("test", it.next() + "");
}

We can test it: 我们可以测试一下:

ArrayList<Float> positions = new ArrayList<Float>();
positions.add(0.123123f);
positions.add(0.2312f);
positions.add(0.323123f);

Iterator<Float> posIter = positions.iterator();
for(int i = 0; i < 100; i++) {
    loop(posIter, positions);
}

You can use the % operator to go back to the beginning of the list list. 您可以使用%运算符返回列表列表的开头。

int nextSpriteX() {
   currentXPositionIndex = (currentXPositionIndex + 1) % xPositions.size();
   return xPositions.get(currentXPositionIndex);
}

Thanks to all who contributed answers, I went with a different approach in the end, in case anyone has a similar issue, this is what I did: 感谢所有提供答案的人,最后我采取了不同的方法,以防万一有人遇到类似的问题,这就是我所做的:

if (xPosition.get(0)<X.size()-1){

xPosition.set(0, xPosition.get(0)+1);

}

else {xPosition.set(0, 1);
}

sprite.xScreen=xPosition.get(xPosition.get(0));

xPosition is an arraylist and the first element is simply a pointer to another element which contains the X Position - so something like: xPosition是一个arraylist,第一个元素只是指向另一个包含X Position的元素的指针-类似于:

xPosition.add(1);  //Element 0 - pointer to subsequent elements (Points to element 1)
xPosition.add(100); //Element 1 - X position 1
xPosition.add(200)l //Element 2 - X position 2
xPosition.add(400); //Elemen 3 - X Position 3

Cheers!

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

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