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;
size = 0;
array = new Object[DEFAULT_CAPACITY];
public BasicArrayList(int capacity)
size = 0;
array = new Object[capacity];
public void add(E element)
array = element;
public E get(int 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!!
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.