Results 1 to 8 of 8
  1. #1
    kosmokh is offline Member
    Join Date
    Jun 2014
    Posts
    10
    Rep Power
    0

    Default List myList = new ArrayList() Java collection types

    Hi Guys,

    I am looking at a snippet of code in my "learning Java 4th edition" by Orielly and there is a small snipped of code which says:

    Java Code:
    Date date = new Date();
    List list = new ArrayList();
    list.add( date );
    ...
    Date firstElement = (Date)list.get(0); // Is the cast correct? Maybe.
    so I am typing the same thing in my compiler in a small Driver class and for some reason I have an error and Im dumbfounded... whats my problem? shouldn't I be able to do this??


  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: List myList = new ArrayList() Java collection types

    I have an error
    Please copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kosmokh is offline Member
    Join Date
    Jun 2014
    Posts
    10
    Rep Power
    0

    Default Re: List myList = new ArrayList() Java collection types

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.
    Norm, it was my own stupid mistake :) it was taking the List CLASS instead of interface

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,039
    Rep Power
    6

    Default Re: List myList = new ArrayList() Java collection types

    You are importing the wrong List. It should be java.util.List. And your book is obsolete (imho). Since Java 1.5, List and other classes part of the Collection hierarchy are generically typed. So you should do it like this:

    Java Code:
    Date date = new Date();
    List<Date> list = new ArrayList<>();
    list.add( date );
    Date firstElement = list.get(0); // no cast needed now
    I recommend you install the lastest version of Java which is 1.8. It has only been out since March 2014 but you may as well get used to the new stuff (like Lambda expressions).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    kosmokh is offline Member
    Join Date
    Jun 2014
    Posts
    10
    Rep Power
    0

    Default Re: List myList = new ArrayList() Java collection types

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.
    However Now that I have fixed that Issue I have a question for you ... Here in my code I am doing

    List<Date> myList = new ArrayList<Date>(); VS ArrayList<Date> myList = new ArrayList<Date>();

    basically from my understanding I am saying my "here I want myList to be an object that can take any method in the list INTERFACE" correct? did I get that right? SO LinkedList is cool with it, Both array list and LinkedList implement all the methods of List INTERFACE, and they have their own ones like "trimtoSize" in case of ArrayList. So myList wont work with TrimToSize. My question is (and its gonna sound dumb) but

    1. SO the whats the "new Arraylist()" for? if this thing is going to only respond to methods in the List INTERFACE why are we declaring it as Arraylist?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,039
    Rep Power
    6

    Default Re: List myList = new ArrayList() Java collection types

    Quote Originally Posted by kosmokh View Post
    Norm, it was my own stupid mistake :) it was taking the List CLASS instead of interface
    Nope! That's not what you were doing wrong. You were using the java.awt.List and not java.util.List.

    Edit: Sorry, we were talking about the same mistake. You were right!

    Regards,
    Jim
    Last edited by jim829; 06-26-2014 at 03:33 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,039
    Rep Power
    6

    Default Re: List myList = new ArrayList() Java collection types

    List is an interface. ArrayList, LinkedList, etc are implementations of that interface. Sometimes implementations offer additional functionality that is not in the interface spec. If you need the functionality, then assign the implementation instance to the implementation class type. If you don't need the functionality, then assign the implementation instance to the interface type.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: List myList = new ArrayList() Java collection types

    Quote Originally Posted by kosmokh View Post
    1. SO the whats the "new Arraylist()" for? if this thing is going to only respond to methods in the List INTERFACE why are we declaring it as Arraylist?
    There are three things going on with this line:
    Java Code:
    List<Date> myList = new ArrayList<Date>();
    First off is this part is declaring a reference variable called 'myList' of type List<Date>.
    Java Code:
    List<Date> myList
    This variable can reference any object of that type, or subclass, or that implements that interface, that exists on the heap (that's where objects are stored).

    Now this part is creating an object on the heap of type ArrayList<Date> (I won't go into generics type erasure here).
    Java Code:
    new ArrayList<Date>()
    The result of that is also a reference to the part of the heap that object exists in.

    Finally this part assigns that new reference to the 'myList' variable:
    Java Code:
    =
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 05-21-2013, 10:21 AM
  2. get specific arrayList from collection
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 03-23-2011, 05:11 AM
  3. java collection arraylist in jdk 1.4
    By jagdish_ in forum Advanced Java
    Replies: 1
    Last Post: 06-12-2010, 03:19 PM
  4. Collection Types
    By DavidG24 in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 06:03 PM

Posting Permissions

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