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 previous()用法及代码示例
- Java ListIterator nextIndex()用法及代码示例
- Java ListIterator add()用法及代码示例
- Java ListIterator next()用法及代码示例
- Java ListIterator hasPrevious()用法及代码示例
- Java ListIterator remove()用法及代码示例
- Java ListIterator set()用法及代码示例
- Java ListIterator hasNext()用法及代码示例
- Java List spliterator()用法及代码示例
- Java List size()用法及代码示例
- Java ListResourceBundle getKeys()用法及代码示例
- Java List retainAll()用法及代码示例
- Java List add(E ele)用法及代码示例
- Java List add()用法及代码示例
- Java List remove(Object obj)用法及代码示例
- Java List get()用法及代码示例
- Java List add(int index, E element)用法及代码示例
- Java List removeAll()用法及代码示例
- Java List listIterator()用法及代码示例
- Java List toArray()用法及代码示例
注:本文由纯净天空筛选整理自 Java ListIterator previousIndex() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。