3
当我阅读jdk源代码时,我找到了注释,但我不确定它为什么在这里使用?
在java中使用“@SuppressWarnings(”unchecked“)”会有什么好处?
我们应该什么时候使用它,为什么?从JDK源代码
示例代码
在java中使用“@SuppressWarnings(”unchecked“)”会有什么好处?
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
[Java中SuppressWarnings(“unchecked”)是什么意思?](http://stackoverflow.com/questions/1129795/what-is-suppresswarnings-unchecked-in-java) – LisaMM