Results 1 to 13 of 13

Thread: Collections

  1. #1
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default Collections

    first of all I would like to excuse me for my utter noobness... But I've got the following:

    List<Integer> myList = new ArrayList<Integer>()

    Now myList is declared as a List but initialized as an ArrayList, my question is, can I use ArrayList methods (not supported by List) on myList?

    Thx in advance
    (PS: it is quit possible that this question is already somewhere on this forum, but after 45 minutes searching I really had enough sorry for that :cool: )

    Peace Out nFm!
    Last edited by NatureFreshMilk; 05-20-2011 at 10:21 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The best way to figure this out would really be to test it. Make a small example which creates an array list as you want to test it. Then perform different array list methods on it, and print the list between each method, finally, see what happens when you compile it.

    Short answer: Yes, you can use arrayList method.


    long(ish) answer: List is actually an interface, which array list implements. If you don't know about interfaces, check this link: What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts), Short explanation of interface: An interface is a non instantiable blueprint(I use blue print because it's sort of a class, but not). It's basically a fully abstract class, none of the methods have bodies and any class that implements them must have an implementation of every method.

    The List can't be instantiated so
    Java Code:
    List<Integer> myList = new List<Integer>();
    would not work.

    Since array list implements this interface it must implement each method in the interface. The methods you called are called correctly thanks to polymorphism. Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    Polymorphism let's you do some interesting things, try this snippet out

    Java Code:
    class A{
      public void print(){
        System.out.println("Class A");
      }
    }
    
    class B extends A{
      @override public void print(){
        System.out.println("Class B");
      }
    }
    
    class C extends A{
      @override public void print(){
        System.out.println("Class C");
      }
    }
    
    public class D extends A{
      @override public void print(){
        System.out.println("Class D");
      }
      public static void main(String[] args){
        A a, b, c, d;
        a = new A();
        b = new B();
        c = new C();
        d = new D();
        a.print();
        b.print();
        c.print();
        d.print();
      }
    }

    After you get the understanding of polymorphism and interfaces head to these two links

    List (Java Platform SE 6)
    ArrayList (Java Platform SE 6)

    These are the API's for List, and ArrayList, compare and contrast which methods array list has that list also has. Find the ones list doesn't have but array list does have(are there any?)

    I hope this helps, post back if you aren't clear on anything.

  3. #3
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default

    Everything cleared out, thank you very much!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome. Please mark your thread solved with the thread tools at the top.

  5. #5
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default

    Hmmmm I was a bit to quick here,
    Let me do this with an example:

    public class Animal{
    public void eat(){...}
    public void sleep(){...}
    }

    public class Kitten extends Animal{
    public void miauw(){...}
    }

    now the following:

    Animal a1 = new Animal();
    Kitten k1 = new Kitten();
    Animal k2 = new Kitten();

    a1.eat() // no problems here
    k1.eat() // no problems here
    k2.eat() // no problems here
    k1.miauw() // works fine
    k2.miauw() // compiler starts to complain becuase it can find symbol method miauw() in class Dier

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The problem here is that we know the class k2 is a kitten, however; the compiler thinks it's an animal, and animal doesn't have the method you want.

    Something like
    Java Code:
    ((Kitten)k2).miauw();
    Should work(test it to make sure).

    If the super class and subclass have the same methods(subclass methods override the superclass ones) the compiler can figure out and use the correct method thanks to polymorphism.

  7. #7
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default

    Indeed it works, so typecasting it will be!!

    Thanx

    (PS I'm looking in thread tools to mark this as solvedn but where the hell is it? Can't find it.. feeling a little bit dumb now)

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by NatureFreshMilk View Post
    (PS I'm looking in thread tools to mark this as solvedn but where the hell is it? Can't find it..
    When you open your own thread, check the thread name, "Collections". Below that there is a toolbar, which has "Thread Tools" drop down menu. Mark it solved from there.

    Or if you still can't find it, then go by the old way. Hit Ctrl+F on whichever browser you are and type "Thread Tools", you will probably find only one match. :p

    Hope that makes it clear,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default

    The Thread Tools I found, but it only gives me four options:
    -Show Printable Version
    -EMail this Page
    -Unsubscribe from thread
    -Add a Poll

    Mark as solved is nowhere to be found =p...
    Well maybe I stumble on it one day or another!

    Peace!

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

    Default

    Quote Originally Posted by NatureFreshMilk View Post
    Indeed it works, so typecasting it will be!!

    Thanx

    (PS I'm looking in thread tools to mark this as solvedn but where the hell is it? Can't find it.. feeling a little bit dumb now)
    If you find yourself typecasting a lot then you may well have a design flaw.

  11. #11
    Join Date
    May 2011
    Location
    Herentals, Belgium
    Posts
    14
    Rep Power
    0

    Default

    @ Tolls

    Yeah I know. But there was an example (actually a few) in my schoolbook that didn't work, and appearantly doesn't make any sense... I tought about pointing out all the mistakes in the book, but I found out that writing a new book would consume less time... :cool:

    And instead of asking the teacher (not the brightest one, god knows where they found him), I preffer asking my questions to a much more experienced, and very helpfull community!!

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I was just about to come here and bring that up. Typecasting, while it works for such a trivial example, it's probably not the best because if you have a method that takes Animal, it can take animal, cat, kitten, dog, puppy, etc. If you typecast it to kitten you run into an error. So be careful not to fall in love with it.


    edit: perhaps the area to mark threads solved was changed along with the forums.

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    NatureFreshMilk,

    In case if you are interested, have a look at this nice article : Casting Reference Variables

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Collections help.
    By YoungJavaBoy in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 12:14 AM
  2. Help collections
    By nikosv in forum New To Java
    Replies: 8
    Last Post: 12-13-2010, 06:29 PM
  3. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 03:46 PM
  4. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 10:15 PM
  5. Performance Of Collections
    By thomasprabu in forum Advanced Java
    Replies: 0
    Last Post: 01-05-2008, 12:17 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
  •