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


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


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 previous() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。