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


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


ListIterator 接口的 nextIndex() 方法用于返回 next() 方法返回的元素的索引。

用法

int nextIndex()

参数

NA

返回

上述方法用于返回 next() 方法返回的元素的索引。仅当列表迭代器位于列表末尾时,该方法也可能返回列表大小。

例子1

import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratornextIndexExample1 {
	public static void main(String[] args) {
	 ArrayList < String > vehicles = new ArrayList < String > (); 
	 vehicles.add("Scooter");
	 vehicles.add("Car");
	 vehicles.add("Truck");
	 vehicles.add("Bike");
	 System.out.println("The vehicles are listed as:"+vehicles);
     ListIterator < String > listIterator = vehicles.listIterator();
     System.out.println("The first Index is given as:" + listIterator.nextIndex());
       listIterator.next();
     System.out.println("The second Index is given as:" + listIterator.nextIndex());
       listIterator.next();
     System.out.println("The third index is given as:" + listIterator.nextIndex());
     listIterator.next();
     System.out.println("The fourth index is given as:" + listIterator.nextIndex());
    }
}

输出:

The vehicles are listed as:[Scooter, Car, Truck, Bike]
The first Index is given as:0
The second Index is given as:1
The third index is given as:2
The fourth index is given as:3

例子2

import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratornextIndexExample2 {
   public static void main(String[] args) {
	   ArrayList < Float > marks = new ArrayList < Float > (); 
	   marks.add(22.4f);
	   marks.add(34.98f);
	   marks.add(12.9f);
	   marks.add(76.4f);
		 System.out.println("The marks are listed as:"+ marks);
	     ListIterator < Float > listIterator = marks.listIterator();
System.out.println("The first Index is given as:" + listIterator.nextIndex());
	       listIterator.next();
System.out.println("The second Index is given as:" + listIterator.nextIndex());
	       listIterator.next();
System.out.println("The third index is given as:" + listIterator.nextIndex());
	     listIterator.next();
System.out.println("The fourth index is given as:" + listIterator.nextIndex());
	    }
	}

输出:

The marks are listed as:[22.4, 34.98, 12.9, 76.4]
The first Index is given as:0
The second Index is given as:1
The third index is given as:2
The fourth index is given as:3

例子3

import java.util.ArrayList;
import java.util.ListIterator;
public class JavaListIteratornextIndexExample3 {
    public static void main(String[] args) {
    	 ArrayList < Integer > marks= new ArrayList < Integer > (); 
    	 marks.add(5);
    	 marks.add(10);
    	 marks.add(15);
    	 marks.add(20);
    	 System.out.println("The ages are listed as:"+marks);
         ListIterator < Integer > listIterator = marks.listIterator();
         System.out.println("The first Index is given as:" + listIterator.nextIndex());
           listIterator.next();
         System.out.println("The second Index is given as:" + listIterator.nextIndex());
           listIterator.next();
         System.out.println("The third index is given as:" + listIterator.nextIndex());
         listIterator.next();
         System.out.println("The fourth index is given as:" + listIterator.nextIndex());
        }
    }

输出:

The ages are listed as:[5, 10, 15, 20]
The first Index is given as:0
The second Index is given as:1
The third index is given as:2
The fourth index is given as:3




相关用法


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