Java Collections 的 emptyListIterator() 方法是一種用於迭代 Java 中沒有元素的 List 的方法。
用法:
public static <T> ListIterator<T> emptyListIterator()
參數:它沒有參數。
返回類型:它將返回帶有空元素的列表。
異常:
- NoSuchElementException -當在給定集合中找不到給定元素時,就會發生這種情況。
範例1:
Java
// Java program to run the iterator with no elements
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create an list iterator
ListIterator<String> iterator
= Collections.emptyListIterator();
// get the elements which is empty
System.out.println(iterator.hasNext());
}
}
輸出
false
範例2:
Java
// Java program to iterate the elements with a list
// of elements. Here we will get an error
// because the list is not empty
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create an array list
List<Integer> data = new ArrayList<Integer>();
// add 2 elements into the array list
data.add(100);
data.add(200);
// iterate the elements
ListIterator<String> iterator
= Collections.emptyListIterator();
// display
System.out.println(iterator.next());
}
}
輸出:
Exception in thread "main" java.util.NoSuchElementException at java.util.Collections$EmptyIterator.next(Collections.java:4191) at GFG.main(GFG.java:12)
範例3:
用於顯示下一個索引和上一個索引元素的 Java 程序。我們可以通過使用 nextIndex() 方法來顯示下一個索引
用法:
iterator.nextIndex()
要顯示上一個索引,
用法:
iterator.previousIndex()
Java
// Java program to display next index
// and previous index element
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create an array list
List<Integer> data = new ArrayList<Integer>();
// add 2 elements into the array list
data.add(100);
data.add(200);
// iterate the elements
ListIterator<String> iterator
= Collections.emptyListIterator();
System.out.println(iterator.nextIndex());
// display previous element
System.out.println(iterator.previousIndex());
}
}
輸出
0 -1
相關用法
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.Collections.frequency()用法及代碼示例
- Java Collections.reverse()用法及代碼示例
- Java Collections.shuffle()用法及代碼示例
- Java Collections singletonMap()用法及代碼示例
- Java Collections min()用法及代碼示例
- Java Collections max()用法及代碼示例
- Java Collections addAll()用法及代碼示例
- Java Collections asLifoQueue()用法及代碼示例
- Java Collections unmodifiableCollection()用法及代碼示例
- Java Collections unmodifiableSortedMap()用法及代碼示例
- Java Collections unmodifiableSet()用法及代碼示例
- Java Collections unmodifiableMap()用法及代碼示例
- Java Collections unmodifiableList()用法及代碼示例
- Java Collections checkedCollection()用法及代碼示例
- Java Collections checkedSet()用法及代碼示例
- Java Collections checkedSortedMap()用法及代碼示例
- Java Collections checkedSortedSet()用法及代碼示例
- Java Collections enumeration()用法及代碼示例
- Java Collections copy()用法及代碼示例
- Java Collections fill()用法及代碼示例
- Java Collections indexOfSubList()用法及代碼示例
注:本文由純淨天空篩選整理自sireeshakanneganti112大神的英文原創作品 Java Collections emptyListIterator() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。