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


Java Enumeration hasMoreElements()用法及代码示例


实现Enumeration接口的对象生成一系列元素,一次生成一个。 hasMoreElements()枚举方法,用于测试此枚举是否包含更多元素。如果枚举包含更多元素,则它将返回true,否则返回false。

用法:

boolean hasMoreElements()

参数:此方法不接受任何内容。


返回值:当且仅当此枚举对象包含至少一个要提供的元素时,此方法才返回true。否则为假。

以下示例程序旨在说明hasMoreElements()方法:
示例1:

// Java program to demonstrate 
// Enumeration.hasMoreElements() method 
  
import java.util.*; 
  
public class GFG { 
  
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public static void main(String[] args) 
    { 
  
        Enumeration Days; 
        Vector week = new Vector(); 
  
        week.add("Sunday"); 
        week.add("Monday"); 
        week.add("Tuesday"); 
        week.add("Wednesday"); 
        week.add("Thursday"); 
        week.add("Friday"); 
        week.add("Saturday"); 
        Days = week.elements(); 
  
        while (Days.hasMoreElements()) { 
            System.out.println("Day = "
                               + Days.nextElement()); 
        } 
    } 
}
输出:
Day = Sunday
Day = Monday
Day = Tuesday
Day = Wednesday
Day = Thursday
Day = Friday
Day = Saturday

示例2:

// Java program to demonstrate 
// Enumeration.hasMoreElements() method 
  
import java.util.*; 
  
public class GFG { 
  
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public static void main(String[] args) 
    { 
  
        Enumeration<Integer> classNine; 
        Vector<Integer> rollno = new Vector<Integer>(); 
  
        rollno.add(1); 
        rollno.add(2); 
        rollno.add(3); 
        rollno.add(4); 
        rollno.add(5); 
        rollno.add(6); 
        rollno.add(7); 
        rollno.add(8); 
        classNine = rollno.elements(); 
  
        while (classNine.hasMoreElements()) { 
            System.out.println("Roll No = "
                               + classNine.nextElement()); 
        } 
    } 
}
输出:
Roll No = 1
Roll No = 2
Roll No = 3
Roll No = 4
Roll No = 5
Roll No = 6
Roll No = 7
Roll No = 8

参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#hasMoreElements()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Enumeration hasMoreElements() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。