Java:list.listIterator()返回的对象属于哪个CLASS?
Java: Which CLASS does the object returned by list.listIterator() belong to?
关于 Java 中迭代的问题。我(有点)熟悉 Iterator、ListIterator 和 Iterable 接口,即我了解它们背后的想法。但这也是我的问题。
如果我有一个 ArrayList 的实例,让我们简单地调用这个实例 'list',然后如果我进行方法调用 'list.listIterator()',那么 CLASS 将执行结果(即返回)对象属于?
我知道它必须是一个实现接口 ListIterator 的 class,但是仍然没有告诉我它属于哪个 CLASS。而且在线文档似乎也没有告诉我这一点。或者它只是一个 'internal' - 因此 anonymous/unnamed - Java class?
谢谢!
荷兰.
你做就知道了
System.out.println(new ArrayList<String>().listIterator().getClass());
您会看到 class 在 ArrayList
内声明并称为 ListItr
。
是private
。这样做有充分的理由。首先,它使 ArrayList
的作者能够在不破坏任何人代码的情况下更改实现。此外,您无需关心实际 class 是什么;重要的是它遵守 ListIterator
.
的合同
在线文档告诉您可以从 API 中得到什么以及您可以做什么,您可以查看源代码以找到您想要的详细信息,所以这里是:
来自Java源代码:
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
上面告诉您将获得 ListItr
的实现,下面是 class 的实际实现:
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
关于 Java 中迭代的问题。我(有点)熟悉 Iterator、ListIterator 和 Iterable 接口,即我了解它们背后的想法。但这也是我的问题。
如果我有一个 ArrayList 的实例,让我们简单地调用这个实例 'list',然后如果我进行方法调用 'list.listIterator()',那么 CLASS 将执行结果(即返回)对象属于?
我知道它必须是一个实现接口 ListIterator 的 class,但是仍然没有告诉我它属于哪个 CLASS。而且在线文档似乎也没有告诉我这一点。或者它只是一个 'internal' - 因此 anonymous/unnamed - Java class?
谢谢! 荷兰.
你做就知道了
System.out.println(new ArrayList<String>().listIterator().getClass());
您会看到 class 在 ArrayList
内声明并称为 ListItr
。
是private
。这样做有充分的理由。首先,它使 ArrayList
的作者能够在不破坏任何人代码的情况下更改实现。此外,您无需关心实际 class 是什么;重要的是它遵守 ListIterator
.
在线文档告诉您可以从 API 中得到什么以及您可以做什么,您可以查看源代码以找到您想要的详细信息,所以这里是:
来自Java源代码:
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
上面告诉您将获得 ListItr
的实现,下面是 class 的实际实现:
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}