Generics: SupressWarnings("unchecked"). Why, and why not in java.util.ArrayList
Hello
I have a problem with generics, which I can solve adding
'@SupressWarnings("unchecked")' before
the methods. I have seen, that the java.util.ArrayList uses a
similar code and does not use this
warning supression.
What could be the problem in the following selfmade List?
Using
SUNs jdk1.6.0_16,
Eclipse Platform 3.5.0.v20090611a-9gEeG1HFtQcmRThO4O3aR_fqSMvJR2sJ
Ubuntu 9.04
Thanks in advance
phi
Here the two compilation Units in the same Package "generics":
**********
package generics;
public interface List <T> extends Iterable<T>{
T getFirst();
void add(T obj);
void removeFirst();
} // end of class List
*********
package generics;
import java.util.Iterator;
/**
* @author Philipp Gressly (phi@gressly.ch)
*/
public class ListImplementation<T> implements generics.List<T> {
Object [] data;
int size;
public ListImplementation () {
this.data = new Object[20]; }
public void add(T obj) {
data[size++] = obj; }
public T getFirst() {
if (isEmpty()) {
return null; }
return (T) data[0]; } // <<---- WARNING
private boolean isEmpty() {
return 0 == size; }
public void removeFirst() {
if(isEmpty()) { return ; }
int toPos = 0;
while(toPos < size - 1) {
data[toPos] = data[toPos + 1];
toPos = toPos + 1; }
size = size - 1; }
@Override
public Iterator<T> iterator() {
return new ListIterator<T>(this.data, this.size); }
} // end of class ListImplementation
class ListIterator<T> implements Iterator<T> {
Object [] objectArray;
int size;
int pos;
public ListIterator(Object [] oarr, int size) {
this.objectArray = oarr.clone();
this.size = size; }
@Override
public boolean hasNext() {
return pos < size ; }
@Override
public T next() {
return (T) objectArray[pos++]; } // <<---- WARNING
@Override
public void remove() { }
}
*********