ArrayList vs LinkedList

In my 2D platformer game, any object that can interact with the world around it is called an Interactable and is expected to be added to a level. So far, I’ve been adding each Interactable into an ArrayList which I constantly loop through to make each Interactable run.

So far, this method hasn’t really faltered because I’m not typically adding and removing objects constantly. I’m considering using a LinkedList, however, seeing as I plan to have Interactables be added and removed at a fast pace. Would using a LinkedList be important in this scenario?

Stick with ArrayList because the objects are put into a backing array, which is faster to loop through than a LinkedList’s nodes.

Also Doesn’t LinkedList leave null spaces in the List if you remove an object? or was that Hashtable…

LinkedList is rarely useful, since it has so much overhead even when it is supposed to be faster. For a very very large list where objects are constantly removed randomly hundreds of times per frame or so, LinkedList might be faster. The problem with LinkedList is that it creates an Entry object which it wraps in each object added to store the next and previous entries. This allocation (and later garbage collection) makes it a lot slower to add and remove stuff than ArrayList in most use cases. ArrayList’s only weakness is removing objects in the beginning or middle of the list, since all following objects have to be shifted to fill the hole created. The longer the list the slower it becomes.

Even when you have lots of stuff to remove, ArrayList can be a good choice. Lists are often used to keep track of game objects. So you say “But wait! The objects will always be added at the end of the list, but as they die they will be removed! I have thousands of objects, so I should use a LinkedList to avoid shifting thousands of objects on every remove()!”. Nope! Most likely the best solution is to use TWO ArrayLists and pingpong your objects between them. By doing that you can avoid all shifting while still getting fast (and random) access to all objects.


ArrayList<GameObject> currentList, nextList;

//Lists created at load time


//Example game loop:

while(true){

    update(); //Updates all objects
    render(); //Draws all objects

}

private void update(){
    for(int i = 0; i < currentList.size(); i++){
        GameObject go = currentList.get(i);
        go.update();
        if(go.isAlive()){
            nextList.add(go);
        }
    }
    
    //Alive objects are now in nextList. Clear currentList.
    currentList.clear();
    
    //Swap current and next list
    ArrayList<GameObject> temp = nextList;
    nextList = currentList;
    currentList = temp;
}


Nope, LinkedList does not leave null spaces. Only arrays do that (obviously). Hashtables/HashMaps KIND OF do that, but that’s simply because the key no longer maps to anything, so it returns null instead. Not really the same in my opinion.

A great deal of shifting can also be avoided by using an ArrayDeque. But using two ArrayLists is also pretty legit, it’s basically like having your own little semispace gc.

Though it has to be said, considering the number of things that might be found in an ArrayList, removing things from the head of the queue is really often so fast you’d never ever notice it. It depends on how many things are likely to be in it…

Consider a real-life example of, hmm… Particles. So you’ve got 5,000 particles in a frame (a high figure, but not unlikely). They’re stored in an ArrayList. Each entry in the ArrayList takes 4 bytes, so that’s 20kb of RAM, fitting neatly in the L1 data cache on many if not all desktops.

Now, imagine the rather unlikely even that every single particle dies one frame, and you discover this as you iterate through the ArrayList (using an integer index, not an iterator, just for absolute efficiency’s sake). You have to shuffle 4,999 particles down 4 bytes. It’s all in the L1 cache so memory access is effectively free to the CPU; you spend 4,999 cycles moving your particles assuming the loop that copies the data is about as simple and efficient as it can be. Then you have to do it again on the next particle. 4998 cycles. Repeat to solve triangular number, approximately (5000x5000)/2, or in the end 12,500,000 clock cycles, without ever having to touch the L2 or L3 caches most likely, let alone system RAM, until the end of the operation.

That’s 12.5m cycles out of your 3.3 million or so you’ve typically got in a single video frame (2GHz core). Oh dear, you just spent 4 frames ditching a mere 5000 particles. Judder. Imagine if you had, somehow, 10000 particles. That’d be nearly a second wasted doing something utterly trivial and would present itself as a horrible jarring delay in your buttery smooth animation.

But what if, weirdly but possibly, the worst case occurred when even going backwards - every other particle died in one frame? Well, then you’d be compacting a slowly more complex list of particles even if you were scanning backwards. Scanning forwards would be exactly the same speed too.

So you’d be crafty and use the third and final technique which is to make a copy of the original arraylist, and copy the surviving particles into it each frame. This performs consistently no matter how many live or dead particles you have in any particular frame or the pattern of their expiration.

Anyway - for the OP - basically you need to do Just That. Each frame, scan your list of Interactables, and copy each live one into a second ArrayList, and then point your game at that ArrayList. Flip between two ArrayLists, alternating each frame, rather than creating (and expanding!) an ArrayList every frame, or that’ll be slow.

Cas :slight_smile:

It seems like ArrayList might not be the best.

How are you removing items from the list? Specifically do you know the item you are removing and then removing it or are you looping through the list to find out which have to be removed?

Interesting first post. Welcome to JGO anyhow! :stuck_out_tongue:

Thank you for the welcome. I despise the first post question. Figure I try to answer some questions and be useful before asking questions myself. :slight_smile:

Look at theagentd’s example code and princec’s in depth explanation for your answer! :]

I’ve been using GapList recently. Until the code that uses the list shows up in a profiler, it’s a good option if you don’t want to worry about the differences between ArrayList and LinkedList.

The solution proposed by theagentd and princec work when every object in the list needs to be checked for state. But DrewLol did not state that that is how he is deciding what gets removed. He might have an object report that it has died and now needs to be removed. In the scenario removing fromt he list involves an O(n) search. In that case a HashSet or HashMap might actually be better because it has O(logn) search time.

[quote=“jmart,post:12,topic:39437”]
How do you mean?

I’m making a guess here, but something like…

You have a system set up so that an object will notify some controller object that it has finished being useful and should be removed from the list. Something like a bullet that’s just discovered that it impacted against a wall. It’ll tell the physics controller “I impacted, remove me from the list of collision objects please!”

In that case, depending on how you wrote your code, the physics controller would have to spend some time searching through its list of objects and remove it.

Since the controller loops through the list of objects in order to call their update() method it can simply check if the object it’s updating has reached its usefulness and discard it right there without looping through the list an unnecessary amount of times per discarded object like theagentd’s code does?

Moreover if the object itself needs to notify the controller you might discard the object mid-update or cause concurrency issues. A situation where the controller is no longer in control but the objects it’s supposed to control pushes the controller around.

I can’t really conceptualize the scenario you’re describing, I could be wrong.

Do you have the code for the implentation? It’s not in Java 6…

[quote=“matheus23,post:16,topic:39437”]
There’s a link at the end of that article. Anyway, you can download it here.

[quote=“Spasi,post:17,topic:39437”]

Wohooow! thank you :slight_smile:

If I have a game that is moving at 30 - 40 frames a second I am not going to be looping through lists of objects needlessly at each of those frames. For the most part nothing is happening of interest during most of those loops. You need to save your CPU for animation, math, collision detection, event handling. A callback to a controller when an event occurs is more efficient.

I was wondering why no one else had steered DrewLols away from using a List at all. Out of all the Java Collections a HashSet seems to be the best option for his scenario. (By the way, hash tables have O(1) add, remove, and query times. Not log n.) In my projects I use a Bag (though slightly different then Kappa’s and more like a special purpose set than a general purpose bag or multiset.)

My class* implements

void add(GameObject o) // Whether I use automatic resizing depends on the project
int size() // Standard element count method
void clear() // Standard remove everything method
void resize(int capacity) // Intended for explicitly changing capacity if I don't use a fixed capacity
Iterator<GameObject> iterator() // See note **
// ** One other method
  • I often work directly with an array
    ** In order to remove “dead” objects I either implement a normal remove(GameObject o) method OR, more frequently, implement something like call removeDeadObjects() or inline code to do the same once per update, OR use Iterator’s remove method since it can be done in O(1) time with one swap.