0

I am trying to iterate over the array list to retrieve the sprites that I have stored in my array list, so I can display them on screen in my do draw method. The code below shows how I have tried to iterate over my array list.

 for (Sprite sprite: ArrayList) {

        ArrayList.get(numSprite);

    }
1
  • So, what is your question? Commented Apr 3, 2015 at 11:06

2 Answers 2

1

First of all, you must have some variable that holds a reference to your ArrayList.

ArrayList<Sprite> listVariable = ...

Your loop will be :

for (Sprite sprite : listVariable) {

}

or

for (int i = 0; i < listVariable.size(); i++) {
    Sprite sprite = listVariable.get(i);
}

If you use the enhanced for loop (the first option), you don't need to call get(index).

Sign up to request clarification or add additional context in comments.

Comments

0

you can try this way:

 Iterator<Sprite> iterator = arayList.iterator();
 while(iterator.hasNext()){
    Sprite item = iterator.next();
 }

To check that there are more items in the list, you have to call iterator.hasNext(), to retrieve the next item, you have to call iterator.next().

or

for(Sprite sprite: arrayList){
 //Do something with sprite object
}

in the for each,u can directly get the object of sprite from arrayList,no need to call arrayList.get() in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.