I have an ArrayStoreException that I do not understand in the following scenario:
file List.java:
import java.lang.reflect.Array;
class List<K> {
K[] _list;
K _dummy;
int _size, _index;
public List(int size) {
_size = size;
_index = 0;
Class<?> cls = getClass();
_list = (K[])Array.newInstance(cls,size);
}
public void add(K obj) {
_list[_index++] = obj;
_list[_index++] = _dummy;
}
}
mainLists.java file:
public class mainLists {
public static void main(String[] args) {
List<String> list = new List<String>(5);
list.add("test");
}
}
What the docs say about ArrayStoreException: "Thrown to indicate that an attempt was made to save the wrong type of an object into an array of objects"
but am I passing a "test type" of type String to add () no?
what is the problem?
THX
Chris
Chris source
share