V
Vikram
Hi,
Looking at the signature of the following method in
java.util.List
public interface List<E> extends Collection<E> {
.........
........
......
<T> T[] toArray(T[] a);
}
I wrote a small program as below:
ArrayList<String> c = new ArrayList<String>();
c.add("Vikram");
c.add("Pyati");
Integer[] i = new Integer[20];
c.toArray(i);
This did not give me a compilation error, though it fails at runtime
giving java.lang.ArrayStoreException, which is perfect.
My question is , why did the above mentioned method be declared as
<E> E[] toArray(E[] a);
which will force the method to take only the array of formal type ( in
this case a String[] ) at the compile time
Looking at the signature of the following method in
java.util.List
public interface List<E> extends Collection<E> {
.........
........
......
<T> T[] toArray(T[] a);
}
I wrote a small program as below:
ArrayList<String> c = new ArrayList<String>();
c.add("Vikram");
c.add("Pyati");
Integer[] i = new Integer[20];
c.toArray(i);
This did not give me a compilation error, though it fails at runtime
giving java.lang.ArrayStoreException, which is perfect.
My question is , why did the above mentioned method be declared as
<E> E[] toArray(E[] a);
which will force the method to take only the array of formal type ( in
this case a String[] ) at the compile time