當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。