Results 1 to 10 of 10
  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default Creating ArrayList

    Hey there.
    I am stuck with creating arraylists.

    I have seen them created with reference to List:
    List myNewList = new ArrayList();

    Also with a reference to ArrayList:
    ArrayList myNewList = new ArrayList();


    My question is:
    Why would you declare an ArrayList with a reference to List?
    Are there any advantages over the second method: "ArrayList myNewList = new ArrayList();" ???

    I am completely new to java, so this question may seem pretty dumb.
    But thanks anyways :D

  2. #2
    shai is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    I don't have the understanding to give you a good answer, but I can tell you that if you read about inheritance and polymorphism you will get your answer.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose your method(s) are only interested in the functionality of a List (the interface) and don't care about the peculiarities of ArrayLists or LinkedLists or whatever (the implementations of the List interface); all they care about is they're dealing with a List. So why not treat a particular implementation as a List? You can do something like this:

    Java Code:
    List<T> list= new ArrayList<T>();
    ... and deal with the list object as if it were just a List<T> (as a matter of fact is is a List<T> object). If you want to, or have to use another type of List<T> in the future, all you have to do is change one line, the line above. As a rule of thumb: consider and object of class C as the uppermost superclass B thereof, i.e. B is an ancestor class (or interface) of C.

    kind regards,

    Jos

  4. #4
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    thanks allot
    I think I understand.
    Just to clarify:

    (1) When writing: List<T> list= new ArrayList<T>();
    Is it true to say:
    Data type is: of type "List interface"?
    Does this mean it's actually an ArrayList object but only the methods declared in the List interface are visible in the object?



    (2) In what situation would it be an advantage to use:

    (i) ArrayList<T> list= new ArrayList<T>();
    rather than

    (ii) List<T> list= new ArrayList<T>()
    ??

    I know you said to use (ii) if your methods don't care about the extra stuff that (i) gives the object.
    But why can't I use: "ArrayList<T> list= new ArrayList<T>();" all the time and just use the stuff I need from it?

    Sorry if my questions are confusing
    I aint sure what I'm talking about
    thanks allot.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Yes, as you define the type of your variable as List<T> the type of the variable is 'interface List<T>'. You can define the type as 'ArrayList<T>' if you want (not much wrong with that) but as soon as you have to change the implementation type, e.g. to 'LinkedList<T>' you have to change it everywhere in your code and recompile everything again. For a small project it doesn't matter much, but what if your code is installed everywhere around the world? Your customers won't appreciate a new version again just because you have changed a bit of it.

    kind regards,

    Jos

  6. #6
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    So you can definitely define a variable of type interface?


    change the implementation type, e.g. to 'LinkedList<T>'
    Can you name a situation where you would have to do that?


    you have to change it everywhere in your code and recompile everything again
    Won't you just have to change it in the declaration of the variable. (So only once) ?


    Thanks again for your time.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dewitrydan View Post
    So you can definitely define a variable of type interface?
    Yes, that's what I was saying all the time.

    Quote Originally Posted by dewitrydan View Post
    Can you name a situation where you would have to do that?
    When you have a better implementation available for the job you can easily change to that one without changing one other line of code.

    Quote Originally Posted by dewitrydan View Post
    Won't you just have to change it in the declaration of the variable. (So only once) ?
    But then again, the variable has to be known by the interface type name, not it's implementing class name.

    kind regards,

    Jos

  8. #8
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    ok, I think I finally get it
    Thanks a bunch!

  9. #9
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    JosAH. Are you talking about something like this?

    Java Code:
    List<String> test = new ArrayList<String>();
    ...
    test = new LinkedList<String>();
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    JosAH. Are you talking about something like this?

    Java Code:
    List<String> test = new ArrayList<String>();
    ...
    test = new LinkedList<String>();
    Yep.

    kind regards,

    Jos

Similar Threads

  1. ArrayList
    By rusBoy in forum Advanced Java
    Replies: 1
    Last Post: 11-12-2009, 11:36 AM
  2. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. Replies: 0
    Last Post: 12-05-2007, 02:09 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06:45 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
  •