Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Just a simple question

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Just a simple question

    When dealing with ArrayLists and LinkedList etc whats best practice and does the difference matter?

    List<xx> l = new LinkedList<xx>();
    List<xx> l = new ArrayList<xx>();

    vs

    LinkedList<xx> ll = new LinkedList<xx>();
    ArrayList<xx> al = new ArrayList<xx>();

    Just want to know out of curiosity

    Thanks.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Just a simple question

    Depends on the application.

    Use the List if you want to allow anything that resembles a list as that variable. Otherwise if you want to limit it to a specific type of list (ArrayList or LinkedList), use the latter.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    newbie (December 5th, 2010)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Just a simple question

    Oh yeah, and as a side note, you want to pick better names. Names such as l or al give the programmer(s) no inclination to what that variable is being used for. Even if you named the variables list or arrayList, these aren't specific to the application and again give no inclination to what that variable is being used for. Try names such as buttons (for a list of buttons), or names (for a list of Strings representing names), etc.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Just a simple question

    To add just a bit, using the List makes the code more abstract and less dependent upon an implementation. There are times where an ArrayList is more appropriate, and times where a LinkedList is more appropriate. Using the List allows portions of code to not even care what the implementation is, allowing you to readily change that implementation whenever the need arises.

  6. The Following User Says Thank You to copeg For This Useful Post:

    newbie (December 6th, 2010)

  7. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Just a simple question

    Also, ArrayList has to be in the same place in memory, just like arrays, (i.e. all the values in the ArrayList have to be right next to each other in memory.) That can cause you to have an OutOfMemoryError or possibly a StackOverflowException quicker than if you had a LinkedList. On the other hand, an ArrayList can be accessed the fastest, due to the closeness in memory location, so it's the fastest of all the Lists and ArrayLists types. LinkedList is like a casette tape, you have to hop from either the start an index or from the index to an index. You can't directly get to it, at least your compiler will hop. It may not seem like it takes a lot of time, but try it with like 1000000 elements and you'll notice.

    ArrayList saves more time on access time than LinkedList, but LinkedList can hold more values than ArrayList, usually.

    "Big O-Notation" AND "LinkedList" AND "ArrayList" - ALOT Search
    Last edited by javapenguin; December 6th, 2010 at 12:07 AM.

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Just a simple question

    Mmm, that's not particularly true. An ArrayList can hold more for a given memory size than a LinkedList because LinkedList nodes require at least 1 pointer on top of each element to hold the address of the next node, or 2 for doubly linked lists.

    The advantage of LinkedLists are that they have O(1) complexity in inserting or removing elements (assuming you've already found the location to insert, otherwise it's O(n), just like for an ArrayList).

  9. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Just a simple question

    Also, ArrayList has to be in the same place in memory, just like arrays, (i.e. all the values in the ArrayList have to be right next to each other in memory.) That can cause you to have an OutOfMemoryError or possibly a StackOverflowException quicker than if you had a LinkedList.
    Not true. Both contain references to the objects added into them, whether they are 'adjacent' in memory or not. Not sure where you are getting your Error/Exception information from....
    On the other hand, an ArrayList can be accessed the fastest, due to the closeness in memory location, so it's the fastest of all the Lists and ArrayLists types.
    ...ArrayList saves more time on access time than LinkedList, but LinkedList can hold more values than ArrayList, usually.
    Not true...ArrayList is not always fastest (and again memory location has nothing to do with it). Try inserting and removing the first element, you will notice that LinkedList is much faster as an ArrayList requires re-indexing everything after the add/remove element. Curious where you heard that LinkedList can hold more values than ArrayList...I've never heard or experienced this, and I am curious.
    Last edited by copeg; December 6th, 2010 at 10:18 AM.

  10. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (December 6th, 2010)

  11. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Just a simple question

    Thank you all for your input, was a good insight into such matters


    Quote Originally Posted by helloworld922 View Post
    Oh yeah, and as a side note, you want to pick better names. Names such as l or al give the programmer(s) no inclination to what that variable is being used for. Even if you named the variables list or arrayList, these aren't specific to the application and again give no inclination to what that variable is being used for. Try names such as buttons (for a list of buttons), or names (for a list of Strings representing names), etc.
    Yeah I realise the need for that, just i quickly made them as a quick example. I'll try to keep code in a presentable manner from now on .
    Last edited by newbie; December 6th, 2010 at 03:00 PM.

  12. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Just a simple question

    I meant for accessing an ArrayList, it's 0(1) for any element.

    LinkedList, for element n/2, it'd be O(n/2) or shortened to O(n)

    Sorting is (at worst) O(log2(n)) for ArrayList

    and O(n) for LinkedList

  13. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Just a simple question

    Quote Originally Posted by javapenguin View Post
    Sorting is (at worst) O(log2(n)) for ArrayList

    and O(n) for LinkedList
    The above is pretty far out of context and a bit off topic. Sorting can be implemented in a number of ways, and the efficiency of those ways can differ dramatically based upon how they are implemented and in part the underlying data structure being sorted. In other words sorting an ArrayList may or may not be at worst O(log2(n)), its all dependent upon the sort.

  14. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Just a simple question

    Wait, you're right. I was looking at some data for TreeSet by mistake.

Similar Threads

  1. [SOLVED] simple question, coding unrelated, how to delete multiple tabs?
    By Perd1t1on in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 07:44 PM
  2. SUPER SIMPLE QUESTION!!!
    By Options in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 2nd, 2010, 09:21 PM
  3. Simple Input Dialog Question
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: March 1st, 2010, 11:10 PM
  4. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM