当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ListIterator previousIndex()用法及代码示例


ListIterator 接口的 previousIndex() 方法用于返回给定元素的索引,该索引由对前一个的调用返回。当且仅当迭代器位于列表的开头时,该方法才可能返回 -1。

用法

int previousIndex()

参数

NA

返回

上面的方法用于返回给定元素的索引,该索引由对 previous 的调用返回。当且仅当迭代器位于列表的开头时,该方法才可能返回 -1。

例子1

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorpreviousIndexExample1 {
   public static void main(String[] args) {
	   ArrayList < Character > characters = new ArrayList < Character > ();
   	characters.add('R');
   	characters.add('E');
   	characters.add('A');
   	characters.add('L');
ListIterator < Character > listIterator = characters.listIterator();
System.out.println("The letters are listed as:"+characters);
       System.out.println("The Previous Index is:" + listIterator.previousIndex());
       listIterator.next();   
       System.out.println("The Previous Index is:" + listIterator.previousIndex());
       listIterator.next();
       System.out.println("The Next Index is:" + listIterator.previousIndex());
       listIterator.next();
       System.out.println("The Next index is:" + listIterator.previousIndex());     
   }
}

输出:

The letters are listed as:[R, E, A, L]
The Previous Index is:-1
The Previous Index is:0
The Next Index is:1
The Next index is:2

例子2

import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorpreviousIndexExample2 {
  public static void main(String[] args) {
	  ArrayList < Long > number = new ArrayList < Long > ();
          number.add(34l);
          number.add(78l);
          number.add(57l);
	ListIterator < Long > listIterator = number.listIterator();
	System.out.println("The numbers are listed as:"+number);
	       System.out.println("The Previous Index is:" + listIterator.previousIndex());
	       listIterator.next();   
	       System.out.println("The Previous Index is:" + listIterator.previousIndex());
	       listIterator.next();
	       System.out.println("The Next Index is:" + listIterator.previousIndex());
  }
}

输出:

The numbers are listed as:[34, 78, 57]
The Previous Index is:-1
The Previous Index is:0
The Next Index is:1




相关用法


注:本文由纯净天空筛选整理自 Java ListIterator previousIndex() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。