ListIterator
implements Iterator<E>一个ListIterator用于序列在对象列表。ListIterator可以向前或向后通过列表。
abstract void |
add(E object)
Inserts the specified object into the list between
next and
previous .将指定的对象插入到下一个和以前的列表中。
|
abstract boolean |
hasNext()
Returns whether there are more elements to iterate.
|
abstract boolean |
hasPrevious()
Returns whether there are previous elements to iterate.
|
abstract E |
next()
Returns the next object in the iteration.
|
abstract int |
nextIndex()
Returns the index of the next object in the iteration.
|
abstract E |
previous()
Returns the previous object in the iteration.
|
abstract int |
previousIndex()
Returns the index of the previous object in the iteration.
|
abstract void |
remove()
Removes the last object returned by
next or
previous from the list.
|
abstract void |
set(E object)
Replaces the last object returned by
next or
previous with the specified object.
|
import android.util.Log;
import junit.framework.TestCase;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
/**
* Created by James on 2016/1/17.
*/
public class ExampleUnitTestTest extends TestCase {
public void testAddition_isCorrect() throws Exception {
ExampleUnitTestTest exampleUnitTest = new ExampleUnitTestTest();
List<String> list = new LinkedList<>();
exampleUnitTest.initial(list);
ListIterator<String> li = list.listIterator();
while (li.hasNext()){
Log.d("debug", li.next().toString() + "-->");
}
li.add("元素5");
li.add("元素6");
Log.d("debug", "--------------------------");
for (String str ;li.hasPrevious();){
Log.d("debug",li.previous().toString()+"-->");
}
Log.d("debug", "--------------------------");
while (li.hasNext()){
Log.d("debug",li.next().toString()+"-->");
}
Log.d("debug", "--------------------------");
for (String str;li.hasPrevious();){
str = li.previous().toString();
li.set(str.replace("元素","元素编号"));
}
while (li.hasNext()){
Log.d("debug",li.next().toString()+"-->");
}
assertEquals(4, 2 + 2);
}
private void initial(List<String> list){
list.add("元素1");
list.add("元素2");
list.add("元素3");
list.add("元素4");
}
}