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.


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

Results 1 to 2 of 2

Thread: New Programmer working with generics!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default New Programmer working with generics!

    Here is my interface:

    public interface BasicList<E>
       public void add(E element);
       public E get(int index);
       public void add(int index, E element);
       public void clear();
       public boolean contains(E element);
       public int indexOf(E element);
       public E remove(E element);
       public E set(int index, E element);
       public int size(); 

    Here is my class that implements it, right now I am only working on add() and get()
    public class BasicArrayList<E> implements BasicList<E>
       private int size;
       private Object[] array;
       public static final int DEFAULT_CAPACITY = 10;
       public BasicArrayList()
          size = 0;
          array = new Object[DEFAULT_CAPACITY];
       public BasicArrayList(int capacity)
          size = 0;
          array = new Object[capacity];
       public void add(E element)
          array[0] = element;
       public E get(int index)
          return array[index];

    My problem I know is that I am trying to return an element and giving it an Object. However, I don't know how I would implement a get that would snatch an element out of an array. Thanks in advance!!

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Missouri, United States
    Thanked 2 Times in 2 Posts

    Default Re: New Programmer working with generics!

    I'm not particularly familiar with generics, but it looks like all you need to do is change your array variable from an Object[] to an E[]. Then initialize the array as a new E[Default_Capacity].

    I hope that helps. There is more information regarding generics here.

Similar Threads

  1. Replies: 7
    Last Post: August 17th, 2013, 07:55 PM
  2. Generics.
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2011, 06:53 PM
  3. Generics
    By Kerr in forum Collections and Generics
    Replies: 2
    Last Post: May 19th, 2011, 06:44 PM
  4. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread