I’ve kind of wondered about that ever since I started doing Java a couple of years ago and looked through people’s code. I’d noticed LinkedLists weren’t really used but never really thought about why. This post made me look into it and now I know it’s because array processing is more cache friendly! Well I’ll be… : Guess I’m a bit behind the cutting edge here. The C++ code I’ve seen and worked on over the years uses plenty of linked lists, but I guess was mostly written before cache architectures became common.
Anyway I took a look at the only place I’ve used a LinkedList in my Java code so far (as the A* open list) and swapped it out for an ArrayList just for kicks. Who knew, the ArrayList is straight away quicker a lot of the time, even though the code was specifically written to work with a LinkedList!!! Haha, what a waste of time those data structure courses were
Having said that, the linked list comes back into its own with a less aggressive heuristic function, and if I put more obstacles in the way, because it gets costly to insert all the new nodes into the array. So now I have to decide if I can be bothered to rewrite or not - if I do I’ll probably home bake something in between using some of the suggestions people have made in this thread.
Either way it’s been a good learning day