I am running into conversion errors with Generics. I can provide the full code if needed but I feel like I am missing something really simple. I am attempting to use a method inside of my class to add all elements of input list to current list, but I am getting conversion errors for E.
Can somebody please point me in the right direction as to what I need to be doing? - I am not new to programming, but Java isn't my first language.
public class ArrayList<E> implements List<E> {
// instance variables
/** Default array capacity. */
public static final int CAPACITY=16; // default array capacity
private E[] data; // generic array used for storage
private int size = 0; // current number of elements
public ArrayList() { this(CAPACITY); } // constructs list with default capacity
@SuppressWarnings({"unchecked"})
public ArrayList(int capacity) { // constructs list with given capacity
data = (E[]) new Object[capacity]; // safe cast; compiler may give warning
}
// public methods
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E get(int i) throws IndexOutOfBoundsException {
checkIndex(i, size);
return data[i];
}
public void add(int i, E e) throws IndexOutOfBoundsException {
checkIndex(i, size + 1);
if (size == data.length) // not enough capacity
resize(2 * data.length); // so double the current capacity
for (int k=size-1; k >= i; k--) // start by shifting rightmost
data[k+1] = data[k];
data[i] = e; // ready to place the new element
//print(data[i].getClass());//STRING BASED ON CURRENT TEST CODE
size++;
}
//-------ERROR CODE
public void addAll(ArrayList l){
//Adds all elements in l to the end of this list, in the order that they are in l.
//Input: An ArrayList l.
//Output: None
//Postcondition: All elements in the list l have been added to this list.
//add(int i, E e)
//l IS ALSO AN ARRAY LIST SO SAME METHODS/VARIABLES APPLY...JUST REFERENCE l'S VERSION
//add(0,"hi");//ERROR NOT E
int foundSize = l.size();
//print(foundSize);
print("SIZE:"+size);
print("LENGTH:"+data.length);//TOTAL
for (int i=0; i < foundSize; i++){
//print(data[i]);
this.add(size(), l.get(i));//INCOMPATIBLE TYPES
}
}
//-------ERROR CODE