Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a for loop over a nested ArrayList?

I'm going to make this 2D rendering engine as modular as I possibly can. I've come up with, but haven't yet finished, a priority list for drawing/updating sprites, so that I can control which sprites are in front of each other.

This code is meant to loop through each priority list and render every sprite in that list.

//I don't entirely understand what this for-each type of loop does.

public static void renderSprites(ArrayList<ArrayList<AbstractSprite>> priorities){
    for (ArrayList<AbstractSprite> priority : priorities){
        for(AbstractSprite sprite : priorities.get(priority)){

           renderSprite(/* what should I reference to get the relevant sprite? */);

           //this is my best guess at what the nested loop would be, but it obviously doesn't work.
          //any ideas?
        }
    }
}
like image 938
Sam Walls Avatar asked Dec 23 '13 20:12

Sam Walls


People also ask

How to iterate ArrayList using for loop and for each loop?

How to iterate ArrayList using for loop and for each loop in Java? You can use the size method of ArrayList to get total number of elements in ArrayList and the get method to get the element at the specified index from ArrayList.

What is nested ArrayList in Java?

Nested ArrayList in Java. In Java, ArrayList is a class of Java Collections framework that provides us with the concept of resizable arrays. It is a list of arrays where we can automatically adjust its capacity by adding or removing elements. It is therefore also known as Dynamic Arrays.

Why loop through the elements of an ArrayList in Java?

Why would be needing to loop through the elements of an arraylist? Well, to make it simple it is for the intention of accessing each member of the the list. Either we would be using the elements to do some complicated logical program or simply just to print it. From official java documentation the following best describes an ArrayList:

How do you iterate through a nested list in Python?

Iterating through a Nested List. Lets us see how a typical nested list looks like : There are multiple ways to iterate through a Nested List: Method 1: Use of the index to iterate through the list. Use of Positive Index: Python3. filter_none. edit close. play_arrow. link brightness_4 code


2 Answers

Should simply be

for (ArrayList<AbstractSprite> priority : priorities){
    for(AbstractSprite sprite : priority){
        renderSprite(sprite);
    }
}

This assumes that the priorities arraylist is sorted in order from high to low priority, so you are rendering the sprites with the highest priority.

Remember that the get() method of an ArrayList expects an int which represents the index of the element you want to access. For-each loops are implemented to make indexing opaque to the user and simply iterate through the Collection instead.

like image 74
C.B. Avatar answered Oct 17 '22 01:10

C.B.


The for..each construct iterates over every element in a collection or an array. The construct is in the form:

for(Type varName : [Array || Collection]){
  //for each iteration varName is assigned an element in the collection/array
}

In your example the outer loop is properly constructed and will assign the ArrayList to the priority variable for each iteration. The inner loop is not properly constructed since you want to iterate over each element in the ArrayList priority, however the code attempts to use an element from the priority ArrayList. The following code shows the proper structure and replaces the ArrayList with the List interface, which is preferred over using the concrete type of the collection.

public static void renderSprites(List<List<AbstractSprite>> priorities){
    for (List<AbstractSprite> priority : priorities){
        for(AbstractSprite sprite : priority)){
           renderSprite(sprite);
        }
    }
}
like image 44
Kevin Bowersox Avatar answered Oct 17 '22 02:10

Kevin Bowersox