ListIterator 接口的 previous() 方法用於返回列表中的前一個元素並將光標向後移動。上述方法可用於反向迭代列表。
用法
public E previous()
參數
NA
返回
上述方法用於返回給定列表的前一個元素。
拋出:
NoSuchElementException- 如果給定的迭代沒有這樣的先前元素。
例子1
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorpreviousExample1{
public static void main(String[] args) {
ArrayList < Long > numbers = new ArrayList < Long > ();
numbers.add(88l);
numbers.add(67l);
numbers.add(57l);
ListIterator < Long > listIterator = numbers.listIterator();
System.out.println("The list of alphabets is given as:"+numbers);
System.out.println("In the forward direction:");
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("In the backward direction:- ");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
輸出:
The list of alphabets is given as:[88, 67, 57] In the forward direction: 88 67 57 In the backward direction:- 57 67 88
例子2
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorpreviousExample2 {
public static void main(String[] args) {
ArrayList < String > appliances = new ArrayList < String > ();
appliances.add("Fan");
appliances.add("Tubelight");
appliances.add("Bulb");
appliances.add("Television");
ListIterator < String > listIterator = appliances.listIterator();
System.out.println("The list of electrical appliances is given as:"+appliances);
System.out.println("In the forward direction:");
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("In the backward direction:- ");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
輸出:
The list of electrical appliances is given as:[Fan, Tubelight, Bulb, Television] In the forward direction: Fan Tubelight Bulb Television In the backward direction:- Television Bulb Tubelight Fan
例子3
import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratorpreviousExample3 {
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 list of alphabets is given as:"+characters);
System.out.println("In the forward direction:");
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("In the backward direction:- ");
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}
輸出:
The list of alphabets is given as:[R, E, A, L] In the forward direction: R E A L In the backward direction:- L A E R
相關用法
- Java ListIterator previousIndex()用法及代碼示例
- 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 previous() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。