ArrayList vs LinkedList Java

This question already has an answer here:

  • When to use LinkedList over ArrayList? 28 answers

  • ArrayList is a list implementation that's backed by an Object[] . It supports random access and dynamic resizing.

    LinkedList is a list implementation that uses references to head and tail to navigate it. It has no random access capabilities, but it too supports dynamic resizing.

    Bear in mind that both support the get(int index) signature, but the difference between the two implementations is performance: with an ArrayList , that's a matter of going to the index position, whereas with a LinkedList , you have to walk down the object chain (either from the front or the rear, depending on what you've indexed into).


    For an arrayList, you have access to every element, which has its own index value. For example, if you want the third item in an ArrayList, you just have to perform arrList.get(2) to get this value. An ArrayList is made using a similar structure as an array.

    For a linked list, you ONLY have access to the first element, but each element has access to the next one. So, to get to the third element, you have to go to the first, then second, then finally third. Think of a LinkedList like a chain. If you have the first part of the chain but cut off its access to the second part, you lose the rest of it too.

    They both have their advantages and disadvantages, in terms of memory, processing time, and ease of use. Let me know if you have any more specific questions or want clarification.

    链接地址: http://www.djcxy.com/p/19976.html

    上一篇: LinkedList与ArrayList的比较

    下一篇: ArrayList vs LinkedList Java