/*! Ads Here */

Does a linked list use more memory? Chi tiết

Mẹo Hướng dẫn Does a linked list use more memory? 2022


Quý khách đang tìm kiếm từ khóa Does a linked list use more memory? được Cập Nhật vào lúc : 2022-01-01 13:40:11 . Với phương châm chia sẻ Mẹo về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc Post vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.


This post will discuss differences between the ArrayList and LinkedList in Java.


Nội dung chính


  • 1. Underlying Data Structure

  • 2. Capacity

  • 3. Memory Overhead

  • 5. Performance


  • 1. Underlying Data Structure


    Both ArrayList and LinkedList are two different implementations of the List interface. ArrayList is a resizable-array implementation, whereas LinkedList is a Doubly-linked list implementation of the List interface.


    2. Capacity


    An ArrayLists capacity least as large as the list size, and it grows automatically as more elements are added to it. Its default capacity is just 10 elements. Since resizing degrades performance, it is always better to specify the initial capacity of the ArrayList during initialization itself.


    On the other hand, a LinkedLists capacity is exactly equal to the list size, and we cannot specify the capacity during the list initialization.


    3. Memory Overhead


    LinkedList comes up with memory overhead since every element is a node object which stores pointers to the next and previous elements. But since the memory needed for each node might not be contiguous, LinkedList wont result in any major performance issues.


    ArrayList, however, needs a contiguous block of memory in a heap for allocating the dynamic array. This might be space efficient but sometimes costs performance issues when the Garbage Collector ends up doing some work to không lấy phí up a necessary contiguous block of memory in a heap.


    4. Caching


    Traversing through the elements of an ArrayList is always faster than a LinkedList. This is because of Sequential locality or locality of reference where hardware will cache contiguous memory blocks for faster read access.


    5. Performance


    We can measure the performance based on the following parameters:


    ⮚ Insertion the end


    add(E element)


    The add operation for an ArrayList runs in amortized O(1) time, but is O(n) in the worst case. The worst-case happens when the maximum capacity of ArrayList is reached, and elements from the old array are copied to the new array, which is double the size of the old array.


    For a LinkedList, add is a O(1) time operation since the specified element is appended the end of the list and pointer to the last node is maintained by LinkedList class.


    ⮚ Insertion the specified index


    add(int index, E element)


    The add operation the specified index is O(n) time operation for an ArrayList as it requires shifting of all elements past the insertion point for accommodating the new element.


    For a LinkedList, insertion the specified index costs O(n) as it has to traverse the list from the beginning or the end to get to the specified index.


    ⮚ Deletion


    remove(int index)


    For both ArrayList and LinkedList, the remove operation takes O(n) time. For an ArrayList, it requires shifting of all latter elements, and for the LinkedList, it has to traverse the list from the beginning or the end, whichever is closer to the specified index.


    ⮚ Read


    get(int index)


    An ArrayList allows fast random read access, so get operation takes only O(1) time.


    For a LinkedList, get operation takes O(n) time since it has to traverse the list from the beginning or the end, whichever is closer.


    Thats all about the differences between ArrayList and LinkedList in Java.


    Rate this post


    Submit Rating


    Average rating 4.9/5. Vote count: 10


    No votes so far! Be the first to rate this post.


    We are sorry that this post was not useful for you!


    Tell us how we can improve this post?


    Submit FeedbackThanks for reading.


    Please use our trực tuyến compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.


    Like us? Refer us to your friends and help us grow. Happy coding


    Reply

    9

    0

    Chia sẻ


    Chia Sẻ Link Down Does a linked list use more memory? miễn phí


    Bạn vừa đọc nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Clip Does a linked list use more memory? tiên tiến và phát triển nhất ShareLink Download Does a linked list use more memory? Free.



    Hỏi đáp vướng mắc về Does a linked list use more memory?


    Nếu sau khi đọc nội dung bài viết Does a linked list use more memory? vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Tác giả lý giải và hướng dẫn lại nha

    #linked #list #memory

*

Đăng nhận xét (0)
Mới hơn Cũ hơn

Responsive Ad

/*! Ads Here */

Billboard Ad

/*! Ads Here */