-2

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
ViaTech
  • 2,143
  • 1
  • 16
  • 51

1 Answers1

0

You are passing a raw ArrayList to your addAll method.

Change

public void addAll(ArrayList l)

to

public void addAll(ArrayList<E> l)
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Quick question, if I wanted to keep my signature: public void addAll(ArrayList l), is that possible? – ViaTech Mar 26 '18 at 16:25
  • @ViaTech if you did that, you would be able to pass to the method an `ArrayList` that contains elements of any type. Therefore you wouldn't be able to add them to your `ArrayList` (unless you cast each of them to E, which may throw ClassCastException in runtime). – Eran Mar 26 '18 at 16:29