ListIterator

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");
    }
}


更多请看http://blog.csdn.net/a597926661/article/details/7679765

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值