📜  Java集合 emptyListIterator() 方法和示例

📅  最后修改于: 2022-05-13 01:54:40.642000             🧑  作者: Mango

Java集合 emptyListIterator() 方法和示例

Java Collections 的emptyListIterator()方法是一种用于迭代Java中没有元素的 List 的方法。

语法

public static  ListIterator 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 iterator
            = Collections.emptyListIterator();
        
        // get the elements which is empty
        System.out.println(iterator.hasNext());
    }
}


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 data = new ArrayList();
             
        // add 2 elements into the array list
        data.add(100);
        data.add(200);      
        
        // iterate the elements
        ListIterator iterator
            = Collections.emptyListIterator(); 
        
          // display            
        System.out.println(iterator.next());
    }
}


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 data = new ArrayList();
  
        // add 2 elements into the array list
        data.add(100);
        data.add(200);
  
        // iterate the elements
        ListIterator iterator
            = Collections.emptyListIterator();
        System.out.println(iterator.nextIndex());
  
        // display previous element
        System.out.println(iterator.previousIndex());
    }
}


输出
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 data = new ArrayList();
             
        // add 2 elements into the array list
        data.add(100);
        data.add(200);      
        
        // iterate the elements
        ListIterator 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 data = new ArrayList();
  
        // add 2 elements into the array list
        data.add(100);
        data.add(200);
  
        // iterate the elements
        ListIterator iterator
            = Collections.emptyListIterator();
        System.out.println(iterator.nextIndex());
  
        // display previous element
        System.out.println(iterator.previousIndex());
    }
}
输出
0
-1