CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Location
    North Carolina
    Posts
    167

    What's the difference between LinkedList and ArrayList?

    ArrayList implements interface RandomAccess, while
    LinkedList not. I've thought LinkedList doesn't support random
    success. However, cgecking its methods, I found out that it DOES!
    Code:
    set(int index, Object element)
    // Replaces the element at the specified position in this list with the specified
     element.
    Then looks like one of them is redundant? Am I right?

    Thank you.
    Last edited by love2mao; June 19th, 2005 at 11:14 AM.

  2. #2
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: What's the difference between LinkedList and ArrayList?

    LinkedList's don't support the notion of constant-time random-access. This means that in order to access any element in the linked list, there's code that has to traverse the list until it comes to the proper node, then do the operation (such as either return the item stored in the node or store a new item). That's why traversing a LinkedList using get() is incredibly inefficient.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  3. #3
    Join Date
    Jun 2005
    Posts
    28

    Re: What's the difference between LinkedList and ArrayList?

    I think the way ArrayList works is that it is basically an array of finite size. Once this array is filled, it stores additional data as a linked list.

  4. #4
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: What's the difference between LinkedList and ArrayList?

    Not exactly, ArrayList's get() method runs in constant time (http://java.sun.com/j2se/1.5.0/docs/...ArrayList.html). Using a linked list just because you need extra space would violate that contract. What usually happens is when an ArrayList's internal storage (usually an array) needs more space, it will create a new larger array, copy the elements over, then do a couple of reference re-assignments.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: What's the difference between LinkedList and ArrayList?

    Quote Originally Posted by love2mao
    ArrayList implements interface RandomAccess, while
    LinkedList not. I've thought LinkedList doesn't support random
    success. However, cgecking its methods, I found out that it DOES!
    Code:
    set(int index, Object element)
    // Replaces the element at the specified position in this list with the specified
     element.
    Then looks like one of them is redundant? Am I right?

    Thank you.
    that an element can be retrieved from the centre of a sequential flow of data does not necessarily confer that the data is accessed randomly (i.e. directly, without need to traverse the stream first)


    a linked list is like a audio tape or a backup tape; you can find a file on it/song on it but you have to stream through from one end to the other. its very quick to add more tape to the end; just splice it in.

    An arraylist is more like a keyboard; you can see the key you want to press, you go straight to it. to add more keys is a major operation
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured