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 9 of 9

Thread: ArrayList implementation

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList implementation

    Hi,

    I need to do the following.

    Suppose I have a class A and class B and C are subtypes of A.

    I want to implement an ArrayList that contains subtypes of A. But I only want to implement this ArrayList using methods in class A.

    I was thinking about using the bounded wildcard, but I could not get it to work.

    So, I need something like:

    public class MyArrayList<? extends A> extends ArrayList<? extends A> {
     
        /**
         * Return a subtype of A
         */
        @Override
        public <? extends A> get(int index) {
            // do something with methods only in A.
        }
     
        /**
         * Add a subtype of A.
         */
        @Override
        public boolean add(<? extends A> e) {
            // do something with methods only in A
        }
     
    }

    Also I want to override the methods get and add in ArrayList. So when I use MyArrayList, I expect to do the following.

     
    MyArrayList<B> bList = new MyArrayList<B>();
    MyArrayList<C> cList = new MyArrayList<C>();
     
    bList.add(new B());
    B b = bList.get(0);
     
    cList.add(new C());
    C c = cList.get(1);

    I could not get this to work. Any ideas how this should be done? Or is this not a good design?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList implementation

    Can't you just use an ArrayList of A?

    ArrayList<A> list = new ArrayList<A>();
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList implementation

    Quote Originally Posted by KevinWorkman View Post
    Can't you just use an ArrayList of A?

    ArrayList<A> list = new ArrayList<A>();
    Then how will the get method now whether to return an object of the type B or C?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList implementation

    Quote Originally Posted by meijuh View Post
    Then how will the get method now whether to return an object of the type B or C?
    It won't. It'll return an A reference, which you can cast to the appropriate type. If you really need to be able to return a B or C from an ArrayList, you're going to have to keep track of two ArrayLists.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList implementation

    Quote Originally Posted by KevinWorkman View Post
    It won't. It'll return an A reference, which you can cast to the appropriate type. If you really need to be able to return a B or C from an ArrayList, you're going to have to keep track of two ArrayLists.
    But this means I need to implement MyArrayList two times for exactly the same functionality. Is there no other way?

    I believe type casting indicates a bad design...

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList implementation

    Why do you need to extend ArrayList in the first place?

    If you need to keep track of two separate types (as opposed to simply keeping track of a single parent type), then you either need two ArrayLists or you need to cast. Simple as that. You said you only need the methods from class A. So why do you care whether something is a B or C?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList implementation

    When adding an element to the ArrayList I know that it is either a B or a C. Before actually adding it I need to check if that B or C is already contained by the ArrayList. So ArrayList.add() which I want to override must invoke a ArrayList.contains or something.

    Hmm... looking at the documentation of ArrayList the method add may not return false. So I can not know if it is added or not.

    I think I need to think a little more about the design.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList implementation

    Why don't you just use a Set?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList implementation

    I moved responsibility from the list to the B and C class. Now the list does not need to know whether the A is in fact a B or C. So this question is no longer relevant.

    Thanks for your help Kevin.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. Help wih implementation...
    By Frank_nor in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 24th, 2009, 05:43 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM