📜  Java中的 StringTokenizer hasMoreElements() 方法及示例

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

Java中的 StringTokenizer hasMoreElements() 方法及示例

StringTokenizer 类hasMoreElements()方法还检查此 StringTokenizer 是否还有可用的令牌。它类似于 hasMoreTokens()。该方法专门存在,以便可以实现该类的Enumeration接口。

句法:

public boolean hasMoreElements()

参数:该方法不带任何参数。

返回值:如果在当前位置之后的字符串中发现至少还有一个令牌的可用性,则该方法返回布尔值 True,否则返回 false。

下面的程序说明了 StringTokenizer 的 hasMoreElements() 方法的工作原理:
示例 1:

// Java code to illustrate hasMoreElements() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer(
                "Lets practice at GeeksforGeeks");
  
        // Counting the tokens
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens());
  
        // Checking for any tokens
        System.out.println(str_arr.hasMoreElements());
  
        // Checking and displaying the Tokens
        while (str_arr.hasMoreElements()) {
            System.out.println("The Next token: "
                               + str_arr.nextToken());
        }
    }
}
输出:
The number of Tokens are: 4
true
The Next token: Lets
The Next token: practice
The Next token: at
The Next token: GeeksforGeeks

示例 2:

// Java code to illustrate hasMoreElements() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer("");
  
        // Counting the tokens
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens());
  
        // Checking for any tokens
        System.out.println(str_arr.hasMoreElements());
    }
}
输出:
The number of Tokens are: 0
false